Ksh script: Run bash-like character substitute: ${@/.txt/}
Question
I have a ksh script that has to remain so (some of the programs it runs insist on being ksh). I want to take the input argument "test.txt" and remove the last 4 characters or find and replace the ".txt" with nothing.
In bash I would do
NewVar=${@/.txt/}
This doesn't work in ksh though. How can I get rid of the .txt in ksh? I tried
bash -c 'NewVar=${@/.txt/}'
but it didn't work the $@ variable into this newly created shell.
Thanks,
Dan
Solution
Why not use basename?
OTHER TIPS
ksh won't let you do it with $@, but if you assign it to a temporary variable, you can do it without spawning another proc with
tmp=$@
newvar=${tmp%.txt}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow