Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute arbitrary command under `bash -c`

Tags:

bash

What is a procedure to decorate an arbitrary bash command to execute it in a subshell? I cannot change the command, I have to decorate it on the outside.

the best I can think of is

>bash -c '<command>'

works on these:

>bash -c 'echo'
>bash -c 'echo foobar'
>bash -c 'echo \"'

but what about the commands such as

echo \'

and especially

echo \'\"

The decoration has to be always the same for all commands. It has to always work.

like image 424
Mark Galeck Avatar asked Apr 30 '15 19:04

Mark Galeck


People also ask

What does [- Z $1 mean in bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.

What is $1 and $2 in bash?

$1 - The first argument sent to the script. $2 - The second argument sent to the script. $3 - The third argument... and so forth. $# - The number of arguments provided. $@ - A list of all arguments provided.

What is $@ in bash?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc.


1 Answers

You say "subshell" - you can get one of those by just putting parentheses around the command:

x=outer
(x=inner; echo "x=$x"; exit)
echo "x=$x"

produces this:

x=inner
x=outer
like image 66
Mark Reed Avatar answered Oct 03 '22 08:10

Mark Reed