82
91

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 5 years have passed since last update.

Shell Script での文字列操作

Last updated at Posted at 2012-12-26

なんかいつもわすれてしまうんだけど。だいたいよくつかうのは ## かなー。

#!/bin/bash

FILE="/usr/local/var.piyo/hoge.piyo"

# 先頭から = #
# 文字列の先頭から、最初に一致するパターンを取り除く (${VAR#pattern})
echo ${FILE#*.} # => piyo/hoge.piyo
echo ${FILE#*/} # => usr/local/var.piyo/hoge.piyo
echo ${FILE#*a} # => l/var.piyo/hoge.piyo

# 文字列の先頭から、最後に一致するパターンを取り除く (${VAR##pattern})
echo ${FILE##*.} # => piyo
echo ${FILE##*/} # => hoge.piyo
echo ${FILE##*a} # => r.piyo/hoge.piyo

# 末尾から = %
# 文字列の末尾から、最初に一致するパターンを取り除く (${VAR%pattern})
echo ${FILE%.*} # => /usr/local/var.piyo/hoge
echo ${FILE%/*} # => /usr/local/var.piyo
echo ${FILE%a*} # => /usr/local/v

# 文字列の末尾から、最後に一致するパターンを取り除く (${VAR%%pattern})
echo ${FILE%%.*} # => /usr/local/var
echo ${FILE%%/*} # =>
echo ${FILE%%a*} # => /usr/loc
82
91
3

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
82
91

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?