0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【PowerShell】変数に入れた文字列でフォルダを作成・移動するワンライナー

Posted at

何を説明する記事か

mkdirで作成したフォルダにcdで移動するワンライナーを書く際、cdに与える文字列が変数の場合でも動作させる方法

コード

$dirName = "goodLuck"; md $dirName | cd

この記事を書くに至るまでの過程

mkdirしたフォルダに飛ぶ方法として、cd $$が存在しているのを知っていた。

md dirName
cd $$

ところが、mdの引数に変数を指定すると、cdした際に変数名を文字列として移動しようとしてしまう。

$dirName = "goodLuck"
md $dirName
cd $$

Set-Location: Cannot find path 'C:\Users\user\source\repos\$dirName' because it does not exist.

mkdirをパイプラインでつないでcdすることで、フォルダを移動することが可能。
ということで、前段のコードに至った。

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?