Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include the `-e` argument in the `$@` bash variable?

Tags:

bash

echo

I'm creating a bash script which will wrap a docker run command, passing in all arguments. The docker cmd being run has a -e parameter like so:

docker run --rm -it --name some_name -v $(pwd):/some_dir some_image some_command -e -r -t

However, the bash script for some reason is not passing in the -e parameter.

For example I have the following test.sh script.

#!/bin/bash
echo "Echoing the command arguments."
echo $@

The -e parameter is not passed through to $@ when in the first position.

$ ./test.sh -e -r -t
Echoing the command arguments.
-r -t

$ ./test.sh -r -e -t
Echoing the command arguments.
-r -e -t

Ultimately I would like to be able to call the docker command as follows, simply passing in all given parameters from the bash script to the docker command.

docker run --rm -it --name some_name -v $(pwd):/some_dir some_image some_command $@

This may be confusing to users if they are expecting the -e parameter to be passed in and the associate activity does not happen.

Is there a way to allow the -e parameter to pass through?

like image 516
hooinator Avatar asked Jul 09 '18 15:07

hooinator


People also ask

How do you pass an argument to a variable in bash?

Passing Arguments as an Array to Variable:Add a default variable “$@” which will store the arguments entered by the user as an array. These arguments will be parsed to variable “array”. The last line will display all the arguments of the variable “array” sorted by the index number. Execute the “./” shell script.

What is E in bash command?

by Aqsa Yasin. Set –e is used within the Bash to stop execution instantly as a query exits while having a non-zero status. This function is also used when you need to know the error location in the running code.

What is the E option in shell?

The -e option means "if any pipeline ever ends with a non-zero ('error') exit status, terminate the script immediately". Since grep returns an exit status of 1 when it doesn't find any match, it can cause -e to terminate the script even when there wasn't a real "error".


1 Answers

Change your test to:

#!/bin/bash
echo "Echoing the command arguments:"
printf ' - %q\n' "$@"

...and you'll see -e present.

A POSIX-conforming implementation of echo will print -e on output when given that string as its first argument. bash's implementation does not comply unless both set -o posix and shopt -s xpg_echo runtime options are set.

like image 146
Charles Duffy Avatar answered Sep 21 '22 12:09

Charles Duffy