cbdeveloper
cbdeveloper

Reputation: 31485

Bash script substring on $(pwd)? Bad substitution error

I'm trying to do a substring on bash and I'm following this Bash scripting cheat sheet.

echo $(pwd)

> Successfully outputs the current working DIR

enter image description here

What am I doing wrong?

I'm trying to slice the $(pwd) at the index 2. I know it's possible to omit the length, to return the rest of the string from that position.

So I'm doing this, and I'm getting the bad substitution error.

echo ${$(pwd):2}

> bash: ${$(pwd):2}: bad substitution

Upvotes: 1

Views: 844

Answers (1)

Raman Sailopal
Raman Sailopal

Reputation: 12895

You are getting confused between command substitution with parameter substitution.

In your particular situation you will need to read the command substitution of pwd into a variable first and then use that for parameter expansion and so:

pworkd=$(pwd)
echo ${$(pworkd):2}

Upvotes: 2

Related Questions