0

I need to run a Bash command with a script calling parameters: comm1 ./script.sh param1 param2 param3. Script includes this: timeout 10 bash -c " comm2 ./param1.param3". It indicates that param1 file is in the current directory.

And it works as long as param1 is a single word. But I need it to be a file name which can have spaces. So command should be: comm1 ./script.sh "param1\ with\ space" param2 param3 . No dot and extension in file name, just spaces are problem and maybe brackets.

I don't know how to modify param1 to be able to use spaces in this case, and if script should also be modified. Thanks for help.

5
  • printf -v cmd_line '%q ' comm2 "$1" "$2" will generate a $cmd_line that's correctly quoted with spaces from $1 or $2 escaped. Thereafter, you can run timeout 10 bash -c "$cmd_line" Commented Jan 31, 2022 at 13:46
  • ...however, I do agree with Kaffe that it's not obvious why you're calling an intermediary copy of bash at all, instead of timeout 10 comm2 "$1" "$2" -- when you don't wrap things in bash -c you don't need the extra escaping in the first place. Commented Jan 31, 2022 at 13:47
  • I call bash first because comm2 ./param1 param2 really is pre-comm2-param comm2 ./param1 param2 meaning command has a special parameter before, so it didn't work all without bash -c. Commented Jan 31, 2022 at 13:56
  • I'd need to know that your "special parameter" really is for that to make sense. A redirection? An environment variable assignment? Either way there's an option available that doesn't require an extra copy of bash (what that option is depending on the details of what the "special parameter" is). Only if it's an exported shell function wrapper do you really, unavoidably need a shell. Commented Jan 31, 2022 at 17:42
  • It's an environment variable for comm2. Commented Feb 1, 2022 at 10:52

2 Answers 2

3

Instead of trying to quote and escape the parameters, send the parameters to bash -c.

timeout 10 bash -c 'comm2 "$@"' bash "$1" "$2"

This will set $0 to "bash" and the first two parameters to the values of the first two positional parameters of the script.

Sign up to request clarification or add additional context in comments.

4 Comments

True, but comm2 (parsed as a shell script, being the argument directly after -c) doesn't inspect $1 and $2 so they just get thrown away. Maybe you want timeout 10 bash -c 'comm2 "$@"' bash "$1" "$2"?
I use parameters like this: timeout 10 bash -c ' comm2 ' ./"param1"."param3" I have to indicate to comm2 that it's a filename. I see echo of timeout 10 bash -c 'comm2' './$1.$2' which may mean that replacement is done correctly, but really it doesn't work. Comm2 doesn't open that file.
@CharlesDuffy: Exactly, copy-paste problem on my side.
Working with comm1 ./script.sh "param1 with space" param2 param3 , thanks.
0

I'm a bit unsure on the exact reason to call bash first.

The easiest answer is to simply skip the bash call:

timeout 10 comm2 "./param with.spaces" param2

If you want to reuse the logic in several places, make a function.

timesup() { timeout 10 "$@" ;}

timesup comm2 "./param with.spaces" param2

whatever command you throw, the function will parse it the same way

If you really need to call bash first for whatever reason, you could mix quotation:

timeout 10 bash -c "comm2 './param with.spaces' param2"

4 Comments

As command is itself a built-in command included in the shell, it might make more sense to leave the OP's distinct placeholder name in place unless you mean to instruct people to use that builtin.
comm is, incidentally, also a standard command)
Hahaha. the first was just a miss. but comm I didn't know off!
Looks simple, but somehow it does OK a few iterations and then just stalls, not sure how. I used comm1 ./script.sh "param1 with space" param2 and saw with echo this: timeout 10 bash -c 'comm2 '\''./param1 with space'\''.param2' It's all single quotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.