0

I have problem running bash command in bash.

Simple:

C="ls"
$C

works

angel@php56:/tmp$ C="ls"
angel@php56:/tmp$ $C
testFile1

Complicated

C="bash -c \"ls\""
$C

works

angel@php56:/tmp$ C="bash -c \"ls\""
angel@php56:/tmp$ $C
testFile1

More complicated

C="bash -c \"bash -c 'ls'\""
$C

doesn't work

angel@php56:/tmp$ C="bash -c \"bash -c 'ls'\""
angel@php56:/tmp$ $C
-c: line 0: unexpected EOF while looking for matching `"'
-c: line 1: syntax error: unexpected end of file

same for C="bash -c \"bash -c \\\"ls\\\"\""

angel@php56:/tmp$ C="bash -c \"bash -c \\\"ls\\\"\""                                                                                                                                                                                               
angel@php56:/tmp$ $C                                                                                                                                                                                                                               
-c: line 0: unexpected EOF while looking for matching `"'                                                                                                                                                                                          
-c: line 1: syntax error: unexpected end of file

Why I can run bash in bash, but cant bash in bash in bash :) May be something with quotes ? Online code: click

1

1 Answer 1

3

The problem is the order of operations with word splitting vs parameter expansion.
It works with eval:

eval "$C"

This gets very tricky very quickly. The best way to deal with dynamic commands that don't include pipes or other redirection is to use arrays:

C=(bash -c "bash -c 'ls'")
"${C[@]}"
Sign up to request clarification or add additional context in comments.

Comments

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.