user2780056
user2780056

Reputation: 3

Bash Substring Removal with Paths

This must be a newbie mistake. I am trying to slice off the head of a path stored as a string. Why does it seem like I can't store the search pattern string in a variable? Looking on the LDP it looks like you can do this.

(http://tldp.org/LDP/abs/html/string-manipulation.html and navigate to substring removal)

test="/Users/Kieran/foo/bar/"

echo ${test#/Users/Kieran}
result: /foo/bar/

test_dir="/Users/Kieran"

echo ${test#test_dir}
result: /Users/Kieran/foo/bar/

Thanks in advance for any responses. Have a good day!

Upvotes: 0

Views: 93

Answers (1)

konsolebox
konsolebox

Reputation: 75458

echo ${test#test_dir}

should be

echo "${test#"${test_dir}"}"

or simply

echo "${test#$test_dir}"

since you wanted to expand the values from test_dir. More about parameter expansion here: http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html

Upvotes: 1

Related Questions