ディレクトリを作成して同時にディレクトリに移動できる1行のコードはありますか?

ディレクトリを作成して同時にディレクトリに移動できる1行のコードはありますか?

私はこれをたくさん繰り返しています。

mkdir longtitleproject
cd longtitleproject

ディレクトリ名を繰り返さずに1行で行う方法はありますか?私はここでパーティーをしています。

ベストアンサー1

これがあなたに必要なシングルライナーです。追加の設定は必要ありません。

mkdir longtitleproject && cd $_

Bashでは、変数は$_前のコマンドで提供された最後の引数です。この場合、作成したばかりのディレクトリの名前です。説明したようにman bash

_         At  shell  startup,  set to the absolute pathname used to invoke
          the shell or shell script being executed as passed in the  envi‐
          ronment  or  argument  list.   Subsequently, expands to the last
          argument to the previous command, after expansion.  Also set  to
          the  full  pathname  used  to  invoke  each command executed and
          placed in the environment exported to that command.  When check‐
          ing  mail,  this  parameter holds the name of the mail file cur‐
          rently being checked."$_" is the last argument of the previous command.

cd $_前のコマンドの最後の引数が指定されたcd !$ため、代わりに前のコマンドcd !$の最後の引数を取得するために使用されます。シェルの歴史から:

cd ~/
mkdir folder && cd !$

ついに家に着きました(または〜/)

cd ~/
mkdir newfolder && cd $_

家の下に新しいフォルダが作成されます! (または ~/newfolder )

おすすめ記事