tcsh: Handle spaces in arguments when passing on to another command
3
votes
1
answer
590
views
I wrote a script that needs to call a command and pass on arguments. My script has its own parameters but some need to be passed through. This fails when arguments to my script have spaces in them.
Here is the script, reduced to the passing on of arguments:
#!/bin/tcsh
set passthrough = ""
checkparams:
if ( $# > 0 ) then
# Add the argument to passthrough, ensuring spaces are preserved
set passthrough = "$passthrough '${1:q}'"
# Show what has been added in one go
echo "Added passthrough: '${1:q}'"
shift
goto checkparams
endif
# Using echo here instead of the real command
echo $passthrough
Calling this script shows the problem:
user@host:/home/user/bin
-> ./test_preserve_passthrough a "b b" c
Added passthrough: 'a'
Added passthrough: 'b b'
Added passthrough: 'c'
'a' 'b b' 'c'
As you can see, the 4 spaces between the 'b's disappear. In fact, when I pass a single argument with spaces in it, echo receives not one argument but many as the single string will be broken up by its spaces " ". I tried many ways of quoting (single, double,
{$x:q}
) and combinations but still cannot preserve the spaces in any way.
When an argument is given to my script like a (TAB-completed) filename this\ \ is\ a\ file\ with\ spaces\ in\ its\ name
the backslashes need to be removed, and in the above example with a "b b" c
three arguments need to be passed on unchanged: a
and b b
and c
.
Asked by Ned64
(9256 rep)
May 19, 2024, 04:42 PM
Last activity: May 20, 2024, 10:26 AM
Last activity: May 20, 2024, 10:26 AM