0
>x="hello"
>y="${x/e/z}"

Expected Output:

>echo $y
>hzllo

Actual Output:

>echo $y
>sh: y="${x/e/z}": The specified substitution is not valid for this command.

Note: I am using an older version of bash. Is there any other way around this? I just want to be able to remove the last 3 characters of a string:

x="file.txt.gz"

I want to create a new variable that is just "file.txt"

I have tried:

y=${x::-3} 

Still gives same error as before.

Ayush Parikh
  • 85
  • 1
  • 6
  • 2
    Use `bash` not `sh`, also the last example does not look right, the `:-` means something else. – Jetchisel May 08 '20 at 00:45
  • You tagged `bash`, isn't that what you're using? It's a `bash` feature. – lurker May 08 '20 at 00:45
  • @lurker i was using .sh – Ayush Parikh May 08 '20 at 00:46
  • @Jetchisel What do you mean? .bash and .sh are giving me both same outputs. – Ayush Parikh May 08 '20 at 00:48
  • Extension does not matter in `unix` even if you put `.py` or `.pl` or `.c` in that script it will not matter at all what matters is the shebang or if you're calling your script like `sh ./my_bash_script_with_a_sh_extention.sh` – Jetchisel May 08 '20 at 00:49
  • @Jetchisel that doesnt answer my question. I can't do string substitution. In the script or outside the script. If I type the first example in terminal. I get sh: y="${x/e/z}": The specified substitution is not valid for this command. – Ayush Parikh May 08 '20 at 00:50
  • 1
    The first message in your error `sh` is crystal clear, `sh != bash` – Jetchisel May 08 '20 at 00:52
  • @Jetchisel what can I do to fix this? – Ayush Parikh May 08 '20 at 00:52
  • `sh: y="${x/e/z}"` means you're using `sh`. Please show what error you get when you use `bash`. If it's the same, then maybe your `bash` is pointing to `sh`. It works fine for me in `bash`. Just to be sure, at the prompt you could enter, `/bin/bash`, and then try your simple case to see if it works. – lurker May 08 '20 at 00:52
  • @lurker how do i default terminal commands to bash instead of sh – Ayush Parikh May 08 '20 at 00:54
  • Your `/etc/passwd` file has the default shell your login uses. What system are you running? The popular ones mostly use `bash` by default. – lurker May 08 '20 at 00:54
  • @lurker its all sh in the /etc/passwd file. can i just change it to bash? how do i do this? – Ayush Parikh May 08 '20 at 00:56
  • `bash -c 'x=hello; y=${x/e/z}; echo "$y"'` compare that to say `sh -c 'x=hello; y=${x/e/z}; echo "$y"'` – Jetchisel May 08 '20 at 00:56
  • 2
    @AyushParikh, ask in your distro forum or at unixstackexchange. – Jetchisel May 08 '20 at 00:57
  • What flavor of linux are you running? Make sure you even have `bash` before changing anything. Try what I mentioned before: enter `/bin/bash` at the shell prompt and see if you get a bash prompt. – lurker May 08 '20 at 00:57
  • @lurker /bin/bash doesnt work. but if i just type bash, itll open a bash terminal and my commands work in there. how can i make all my bash scripts use this – Ayush Parikh May 08 '20 at 00:58
  • `type -a bash` should tell you the absolute path. So you can use that absolute path in your script as the shebang or use `env` – Jetchisel May 08 '20 at 01:01
  • Re: making your scripts work, make sure they start with `#!/bin/bash`, not `#!/bin/sh`. And don't run them with `sh yourscript`; just `yourscript` or `bash yourscript` will do the right thing. – Charles Duffy May 08 '20 at 01:31

0 Answers0