LoginSignup
3

More than 5 years have passed since last update.

文字列を任意のデリミタで区切って最後の要素を取得する

Last updated at Posted at 2014-11-13

ファイルパスをスラッシュで区切ってファイル名だけ取得したかったので、備忘録として。

実装例

#!/bin/bash

delimiter=/
str=/home/hoge/foo/bar.txt

last=${str##*${delimiter}}
echo $last
出力結果
bar.txt

その他の使い所

カンマ区切りの最後の要素を取得したい時は、デリミタをカンマに変えればOK

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
3