Skip to content
Search

Bash:ParameterSubstitution

lastmod 2025-12-30created 2015-12-0217 edits

Notes on parameter expansion (Parameter Expansion).

List

Syntax

Description

EXAMPLE

${변수}

Same as $variable, but some things only work with {}

echo ${string}

${변수:시작위치}

Extracts the string after the start position

echo ${string:4}

${변수:시작위치:길이}

Extracts a string of the given length after the start position

echo ${string:4:3}

${변수:-단어}

Sets a default value when the variable is undeclared or NULL; positional parameters cannot be used

echo ${string:-HELLO}

${변수-단어}

Sets a default value only when the variable is undeclared; positional parameters cannot be used

echo ${string-HELLO}

${변수:=단어}

Sets a default value when the variable is undeclared or NULL; positional parameters can be used

echo ${string:=HELLO}

${변수=단어}

Sets a default value only when the variable is undeclared; positional parameters can be used

echo ${string=HELLO}

${변수:?단어}

Prints the word and exits the script when the variable is undeclared or NULL,

echo ${string:?HELLO}

${변수?단어}

Prints the word and exits the script only when the variable is undeclared

echo ${string?HELLO}

${변수:+단어}

Uses the word only when the variable is declared

echo ${string:+HELLO}

${변수+단어}

Uses the word when the variable is declared or NULL

echo ${string+HELLO}

${#변수}

String length

echo ${#string}

${변수#단어}

Deletes the shortest match of the word from the start of the variable

echo ${string#a*b}

${변수##단어}

Deletes the longest match of the word from the start of the variable

echo ${string##a*b}

${변수%단어}

Deletes the shortest match of the word from the end of the variable

echo ${string%b*c}

${변수%%단어}

Deletes the longest match of the word from the end of the variable

echo ${string%%b*c}

${변수/찾는단어/변경단어}

Replaces the first matching word

echo ${string/abc/HELLO}

${변수//찾는단어/변경단어}

Replaces all matching words

echo ${string//abc/HELLO}

${변수/#찾는단어/변경단어}

Replaces if the start matches

echo ${string/#abc/HELLO}

${변수/%찾는단어/변경단어}

Replaces if the end matches

echo ${string/%abc/HELLO}

${!단어*} or ${!단어@}

Extracts the names of declared variables that contain the word

echo ${!string*} or echo ${!string@}

Test code: string="abc-efg-123-abc"

Upper/lowercase conversion

Since Bash 4.0, it can be done as follows.

Lowercase:

a="TeSt VaLuE"

## 소문자
echo "${a,,}"

## 대문자
echo "${a^^}"

If the syntax above does not work, use awk:

echo $a | awk '{print(tolower($0))}'

If there is no awk either, use tr:

echo $a | tr [:upper:] [:lower:]

Splitting file names

Given FILE=/home/username/Project/scot.remote/20230727/cvt_pm_23-long.mp4, using "${FILE##*/}" leaves only cvt_pm_23-long.mp4.

See also

Favorite site

References


  1. Bash_summarizes_the_key_for_beginners.pdf ↩

author Zer0 sitemap