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?

現在のディレクトリ(パスなし)を取得する

Last updated at Posted at 2025-03-28

ほかにいい方法があるならぜひ教えていただきたく。

pwd | awk -F'/' '{print $NF}'

または

pwd | sed -r 's/^.*\///'

どちらがいいんだろうか。
もしくはもっとよいやり方があるのだろうか。


情報提供いただきました!

変数に突っ込む場合、

baseline = ${PWD##*/}

でいけるそうです。

変数 PWD にはおそらく pwd コマンドと同様の結果が格納されております。
そして ## が変数を前方最長一致で除去してくれるので、最後の / まで除去してくれるんですね。すごい。 * はワイルドカードだ。

もう一つ、

basename -- "$PWD"

で標準出力が可能らしいのですが、 -- がわからなさすぎてまた編集します。。。
なんとなく集合演算というか、論理積なのかな?と思っていますが。。。
または単なる差分なのか

で標準出力が可能です。
-- はオプションの終わりを表します。
git diff -- [filename] などでよくお世話になっていました。

コメントでのご教示、ありがとうございました!

0
0
5

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?