1

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
cbdeveloper
  • 27,898
  • 37
  • 155
  • 336
  • ${ } seems to be expecting a variable and not a value. $(pwd) seems to be a string. Maybe you'll need an intermediate variable or another way to check for pwd: DIR=$(pwd) && ${DIR:2} – fernand0 Nov 24 '20 at 08:57

1 Answers1

2

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}
Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18
  • 2
    Of course, Bash already has `$PWD` which is a "magical" built-in; so you can say `echo "${PWD:2}"` directly. (Notice also [When to wrap quotes around a shell variable](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable)) – tripleee Nov 24 '20 at 09:16