sh , Running stored command with environment variables


Running stored command with environment variables

Question:

Tag: sh

This works in sh:

$ export command="true"
$ "$command"

If I need to pass some variable to the command, however, I can't get it to work:

$ export command="env FOO=\"bar\" true"
$ "$command"
sh: 2: env FOO="bar" true: not found

What is missing?


Answer:

Since you've put $command in quotes this prevents the shell from performing field splitting on it's contents, it's treated as one single word. Thus the name of the of command it tries to run is the full contents of $command including it's spaces and double quote characters.

You could omit the quotes and this almost does what you want:

export command="env FOO=\"bar\" true"
$command

Except this will set FOO to "bar" (with the double quotes) and not bar (without the quotes) as you probably expected. It also won't work if you try to set FOO to something with a space in it:

$ command="env FOO=\"bar foo\" true"
$ $command
env: foo": No such file or directory

The problem here being that double quote characters in the variable command are treated like any other character after being substituted. They aren't removed nor do they prevent field splitting.

What you can do instead is use the eval command:

command="env FOO=\"bar foo\" true"
eval "$command"

This causes the shell to completely re-evaluate everything after eval again and execute as a command. In other words after it does substitution, splitting, expansion, and quote removal on eval "$command" it then does substitution, splitting and expansion and quote removal on env FOO="bar foo" true as if it were the command entered into the shell.

However the eval command is very dangerous. You have to trust everything you pass to it. If you pass something that came from an untrusted user than the user can craft a string that will let him run any command he wants.

As an alternative to eval you can use either alias or a function. The alias command is just as dangerous as eval, but if you're only going to be using $command in from the command prompt and never from scripts then using an alias is the easiest and simplest solution:

$ alias mycommand="env FOO=\"bar\" true"
$ mycommand

If you're using it in shell scripts or both from the command line and in shell scripts I would use a function:

myfunc() {
    env FOO="bar" true
}
myfunc

As an aside you don't actually need to use env to set an environment variable for a command when using one of the Bourne shells, you can just set the variable:

FOO="bar" true

When used like this the variable assignment works like with the env command, it doesn't change the value of FOO in the shell's environment, only in the true command's environment.


Related:


How do I indirectly assign a variable in bash to take multi-line data from both Standard In, a File, and the output of execution


linux,bash,shell,scripting,sh
I have found many snippets here and in other places that answer parts of this question. I have even managed to do this in many steps in an inefficient manner. If it is possible, I would really like to find single lines of execution that will perform this task, rather...

Shell script to build CLI args for a PERL script


sh,double-quotes,single-quotes
I have a Jenkins job, triggered as a parameterized build. It accepts an optional String parameter (HOSTNAMES) that can be a comma separated list of hostnames. I need to pass this comma separated list of hostnames as a command line argument to a PERL script (within Execute shell build step)....

How can I run a docker container and commit the changes once a script completes?


bash,docker,sh,commit
I want to set up a cron job to run a set of commands inside a docker container and then commit the changes to the docker image. I'm able to run the container as a daemon and get the container ID using this command: CONTAINER_ID=$(sudo docker run -d my-image /bin/sh...

sh script vulnerability in Linux


linux,security,sh
I was given an assignment for my Computer Security class. We we were given a piece of code to analyze and determine the vulnerabilities that it might have. #!/bin/sh # shell script to create a copy of the shadow file to the /tmp directory echo > /tmp/shadowcopy # allow only...

Writing a bash script from another script in literal mode


linux,bash,shell,scripting,sh
In a bash script I'm creating another bash script to launch a program: $optionsfile="options.conf" tee "launch-script.sh" > /dev/null <<EOF #!/bin/bash bash $binFile `cat $optionsfile` & EOF I want the output script launch-script.sh to be like this: #!/bin/bash bash $binFile `cat options.conf` & So I would like to have the script...

How can `time` control other processes trough a pipe?


time,pipe,sh
In the general case we have a command like P | Q | R which has the following semantics for the shell: Create a processes P, Q, R Bind the output of P to the input of Q Bind the output of Q to the input of R Bind the...

Shell command for character replacement


linux,shell,sh
i have this string: "abcdef" and i would like to replace each character with another string. I made it with sed but is making a conflict and if on the replaced string there is another character for example the a it replaces again the a with the string. for example:...

How to write and match regular expressions in /bin/sh script?


regex,shell,unix,sh
I am writing a shell script for a limited unix-based microkernel which doesn't have bash! the /bin/sh can't run the following lines for some reasons. if [[ `uname` =~ (QNX|qnx) ]]; then read -p "what is the dev prefix to use? " dev_prefix if [[ $dev_prefix =~ ^[a-z0-9_-][email protected][a-z0-9_-"."]+:.*$ ]]; then...

SIGHUP signal handler reset to default when using system() on embedded linux with busybox


c,linux,sh,busybox
I am working on an embedded linux with busybox. As part of my application I have a rc.init script E80-startmyprog. That script is calling my program prog. trap "" HUP startmyprog >${LOGFILE} 2>&1 </dev/null & startmyprog() { prog } In my program I can see that the signal handlers are...

Order lines by number of occurrences


list,shell,sh
Given a list with one element per line (with occasionally some blank lines), e.g.: 22008 6881 6881 22008 6881 22008 22008 6881 56515 8080 8080 56515 22008 45682 45682 22008 I would like to get as output a list with unique items sorted by number of occurrences: 22008 - 6...

How to execute command when df -h return 98% full


linux,shell,sh
How to execute command when df -h return 98% full I have a disk which is by the /dev/sdb1 917G 816G 55G 94% /disk1 If its return 98% full, I would like to do the following find . -size +80M -delete How do I do it, I will run the...

How to append entry the end of a multi-line entry using any of stream editors like sed or awk


linux,bash,awk,sed,sh
I am trying to use a script to append the host name at the end of a multi-line entry of a specific Host_Alias field in sudoers file. The current sudoers file has an entry similar to : Host_Alias srv_linuxestate= \ host10,host12,host13,host1,host50,\ host16,host1,host2,host11,host15,host21,\ host3,host14 My required output would be something like...

How to negate multiple conditions in the Bourne shell


shell,sh
I have a simple Bourne shell script like this: if ! pwd && ls && mkdir /opt/test/; then echo "failure" else echo "success" fi I want it to report a failure if any of the commands does not exit successfully e.g. if the mkdir command fails due to insufficient permissions....

Printing a newline


bash,sh
GAAAAAAAAAAAAAAH I don't know why I'm struggling so much with this. All I want to do is make: file="$a"[command-here]"$b" to make $file= a [newline] b And yet no matter WHAT echo or printf I try to use, I cannot get it to work!! I just want a newline between a...

Linux - sh script - download multiple files from FTP


linux,ftp,sh
I need script that can download files from ftp via my sh code I have use expect with ftp, but if I do for loop inside code, I got wrong # args: should be "for start test next command" while executing "for v in "a b c"" My code /usr/bin/expect...

Increment of multiple file prefixes?


bash,sh
I am looking for a way in Bash to rename my file prefixes. These files are all in one folder. No other files will be in it. 00 - Artist - Title.mp3 01 - Artist - Title.mp3 ... and so on to 01 - Artist - Title.mp3 02 - Artist...

How to split and iterate substrings by delimiter in BOURNE SHELL?


string,shell,loops,split,sh
Recently I've been trying to customize my shell profile located at ~/.profile in Ubuntu. One thing I want to do is: # PATH-like variable containing paths separated by ':' MY_ROOTS=/f/o/o:/b/a/r:/e/t/c for some magic iterating my_root in $MY_ROOTS do; my_bin="$my_root/bin" # add it to PATH! [ -d "$my_bin" ] && {...

How can I detect if some file in a subdir changed?


linux,console,sh
Basically I have a big directory (>10GB)(each subdir only has a few other subdirs) and want to do sth to any files that changes. I can't just go through all the files and check, because it takes to long and uses about 90% of my CPU :(. This is how...

ANSI escapes don't work in `printf`


bash,printf,sh,ansi-colors
When trying to use ANSI color escapes from a shell script, I was completely stuck, as the escape sequences (\e) were printed verbatim to the output. #!/bin/sh GREEN="\e[32m" RED="\e[31m" CLEAR="\e[0m" printf "${GREEN}test passed${CLEAR}\n" printf "${RED}test failed${CLEAR}\n" Produces \e[32mtest passed\e[0m \e[31mtest failed\e[0m ...

Disregard word order when comparing two variables


shell,unix,sh
I want to compare two variables using shell. $a="abc pqr mno xyz" $b="mno pqr xyz abc" Here two variable values are same (just words order is not same inside the variable). If i compare $a and $b it will say that $a and $b are not same, but I want...

Why doesn't ${#$2} work?


bash,sh
I have this line in a small bash function: if [ ${#$2} -lt 25 ]; then But it gives me Bad substitution on the ${#$2}. What am I doing wrong there? I'm trying to get the length in characters of the second command line argument. How can I do this?...

BASH convert uppercase to lower case and vice versa at same time


bash,shell,sh
How do i create a program that converts uppercase letters to lower case and vice versa at same time from a given file? I know how to convert each independently #!/bin/sh #Program convert upper to lower cat $1 | tr '[:upper:]' '[:lower:]' #!/bin/sh #Program convert lower to upper cat $1...

how to deletes line from a text file that are taken from another file [duplicate]


shell,awk,sed,grep,sh
This question already has an answer here: Remove duplicates from text file based on second text file 4 answers I have a data.txt file with a lot of lines in it and a lines.txt that contains some lines. I want to delete all lines from data.txt that match any...

Confirming Shell Script Input


bash,shell,sh
I'm writing a script to preform a repetitive task that only changes basic values and locations such as user name. I've written up code that prompts for a username and verifies that it is not already in use. I'm now trying to prompt the user if the input the script...

Storing data in shell


shell,sh
I have a need to store a list of e-mails in a shell script. This script will get called and passed a customer number. depending on the customer number I want to populate a variable based on the passed in customer number. I am not sure how to accomplish this...

Bash if number greater then failing


bash,sh
Can anyone explain to me why this is failing? 0111 is greater then 99 but i'm not getting an Invalid database message. db=0111 echo $db £ Verify db is within the correct range if [[ $db -lt 0 ]] || [[ $db -gt 99 ]]; then echo "Invalid database" fi...

source: not found error while running shell script on ubuntu 14.04


django,bash,cron,sh,ubuntu-14.04
OS- Ubuntu 14.04 This is my shell script which i want to schedule using cron job. parser.sh #!/bin/bash source .profile workon venv cd /home/ashish/deployments/myproject export DJANGO_SETTINGS_MODULE=myproject.settings_production python /home/ashish/deployments/myproject/myproject/manage.py parse_data Cron job- */15 * * * * . /home/ashish/parser.sh Whenever this cron job runs i receive a mail with errors: /bin/sh:...

Bash Script_ Inserting variable read from read command into command


linux,bash,curl,sh
I am working on a project using the BLS API. Reference: http://www.bls.gov/developers/api_unix.htm My current script follows: declare id="" echo -n "Enter id: " read id echo -n "enter startyear: " read startyear echo -n "enter endyear: " read endyear curl -i -X POST -H 'Content-Type: application/json' -d '{"seriesid": ["$id"], "startyear":'$startyear'...

shell command to skip file in sequence


shell,sh
I run a C++ code for several data files in sequence using: for i in $(seq 0 100); do ./f.out c2.$(($i*40+495)).bin `c2.avg.$(($i*40+495)).txt; done` Now if some input files are missing, say c2.575.bin is missing then the command is not executed for the rest of the files. How could I modify...

Getting value from function, use that value to get colored output in shell


shell,sh,zsh,zshrc,oh-my-zsh
(Using zsh) I am just trying to get random color output whenever I open a new tab in my terminal. To achieve this I wrote the following shell script, but it is not working as expected: #Standard Colors red='\033[0;31m' NC='\033[0m' # No Color black='\033[0;30m' blue='\033[0;34m' green='\033[0;32m' cyan='\033[0;36m' purple='\033[0;35m' yellow='\033[1;33m' lgreen='\033[1;32m'...

Get value of variable with the aggregated values of other variables [duplicate]


bash,sh
This question already has an answer here: Bash, Concatenating 2 strings to reference a 3rd variable 2 answers Given: export A='TEST_' export B='VAR' How would I get the value of $TEST_VAR in this case? Some more conditions: should work both on sh and bash of the latest versions. should...

Arguments with spaces are seen as separate arguments and not as one argument [duplicate]


linux,bash,shell,sh
This question already has an answer here: Handle whitespaces in arguments to a bash script 3 answers I have the following functions func() { shift 2; func2 [email protected]; } func2() { echo $1; echo $2; } func 1 2 "3 3" "4 4" I exepect that func2 will dispaly...

Bash script - Auto fill answer


linux,bash,shell,sh,auto
I have a bash script that has several questions, is it possible to automatically fill the answers ? ./script.sh install answers in order y 2 1 n n How can I do that in bash ? edit: is it possible to only pass the first answer ? echo "y" |...

Running an executable by calling it in a .sh file


bash,sh
I am very new to bash and using .sh files. I am trying to run the program aescrypt by calling it in a .sh file as follows (aescrypt is in the same directory as the .sh file) : ./aescrypt -e -p password file.txt It throws the following error: ./aescrypt no...

Why does Scala use a reversed shebang (!#) instead of just setting interpreter to scala


bash,scala,sh
The scala documentation shows that the way to create a scala script is like this: #!/bin/sh exec scala "$0" "[email protected]" !# /* Script here */ I know that this executes scala with the name of the script file and the arguments passed to it, and that the scala command apparently...

Redirect stdout using exec in subscript


linux,bash,redirect,command-line,sh
I have one main script which is started as an service. I can't modify this main script, because it is often updated. This main script starts a program, which echo any log to stdout. So i can't see any log of this program. But this main script calls at the...

Issues with Screen - Running Minecraft in a while loop through a screen session


bash,while-loop,sh,minecraft
So Earlier today, I changed my startup scripts for my minecraft server. Problem is, after about 3 hours, the screen for it that was open vanished and refuses to appear when using -ls. The server is still running and from what I can see below, the screen process should still...

How to limit the CPU usage of a dash loop that listens for a filesystem change?


loops,sh,cpu-usage,dash
I wrote a loop in dash which waits for a file to appear in a certain location on the filesystem. But when it runs it takes up 50% of the CPU. I first lowered the CPU usage using a sleep command but the minimal sleep time in dash (1 second)...

Testing truth/falseness in bash script


osx,bash,shell,sh
For some reason, I can't figure out how to test truth in bash: #!/bin/bash FORCE_DELETE="" BE_VERBOSE="" OPTIND=1 while getopts ":fv" FLAG "[email protected]" ; do if [[ "$FLAG" == "f" ]] ; then FORCE_DELETE="true" fi if [[ "$VALUE" == "v" ]] ; then BE_VERBOSE="true" fi if [[ "$FLAG" == "?" ]]...

Syntax error: “fi” unexpected (expecting “then”) in bash script


bash,command-line,scripting,sh
I try to do the script: #!/bin/bash IP='192.168.1.1' fping -c1 -t300 $IP 2>/dev/null 1>/dev/null if [ "$?" = 0 ] then echo "Host found" else echo "Host not found" fi and i turn it: [email protected] ~ $ sh /home/pi/sh/test.sh /home/pi/sh/test.sh: 9: /home/pi/sh/test.sh: Syntax error: "fi" unexpected (expecting "then") where is...

Pipe that does nothing


bash,sh,ksh,nop
I'm on a AIX box and need a program that when used after a pipe does nothing. I'll be more exact. I need something like this: if [ $NOSORT ] ; then SORTEXEC="/usr/bin/doesnothing" else SORTEXEC="/usr/bin/sort -u" fi # BIG WHILE HERE do done | SORTEXEC I tried to use tee...

How to set the timer in shell


shell,sh
Can we set the timer in shell script? means if I set the timer for 20 secs my shell scripts should be execute like this : 20..19..18..17.. after completing the I should get the result. Is it possible ? Please assist me....

Showing progress as shell script runs


shell,sh,snmp,net-snmp
I would like to display something showing that the script is still gathering information from a device. Whether it be hash marks or periods. As you can see below when it starts its walk of a device it echos its start time. It is here I would like to show...

How to extract value of root variable from kernel commandline


linux,bash,sh,busybox
I want to extract the value set to the root variable in following kernel command line, I don't want to use awk column number. console=ttyO0,115200n8 noinitrd mem=256M root=/dev/mmcblk0p2 rw rootfstype=ext4 rootwait=1 ip=none I am able extract it using following command but it depends on the position of root variable but...

POSIX Shell backslash confusion


shell,sh,backslash
I am trying to create a shell script with a simple functionality, but it seems I can not wrap my head about how to handle backslashes correctly. One of my functions look like this: #!/bin/sh func() { cmd="$*" printf "%s\n" "$cmd" echo -e $cmd } func '%{NAME}\\n' This is the...

Sort and counting a column without uniq in bash


linux,bash,sorting,count,sh
I want to add a count of only the first column using bash, without doing uniq, like this: input: 58311s2727 NC_000082.6 100.00 50 58311s2727 NC_000083.6 100.00 60 58311s2727 NC_000084.6 100.00 70 58310s2691 NC_000080.6 100.00 30 58310s2691 NC_000081.6 100.00 20 58308s2441 NC_000074.6 100.00 50 output: 3 58311s2727 NC_000082.6 100.00 50 3...

Linux shell file. mkdir and pushd commands not doing what I'd like


linux,shell,sh,mkdir
I'm trying to create a directory if it does not exist, and the further down the line, create a log file in that directory. It would be in the current users home directory In pseudocode: If directory "/home/LOGS" does not exist make directory "/home/LOGS" Then whenever a log file is...

Getting duplicate output


bash,scripting,sh
I'm trying to write a bash script that looks for a file in one directory and replaces it with one of the same name from a source directory. When I run the find command, it seems to be setting my $path twice for f in build-res/$1/*.png; do file="$(basename "$f")" echo...

Environmental variable is empty into shell script but not in shell


android,bash,shell,sh,libvlc
A shell script checks some of my environmental variables, but this code: if [ -z "$ANDROID_NDK" -o -z "$ANDROID_SDK" ]; then echo "You must define ANDROID_NDK, ANDROID_SDK before starting." echo "They must point to your NDK and SDK directories.\n" exit 1 fi prints out You must define ANDROID_NDK, ANDROID_SDK before...

Validating file records shell script


linux,bash,shell,unix,sh
I have a file with content as follows and want to validate the content as 1.I have entries of rec$NUM and this field should be repeated 7 times only. for example I have rec1.any_attribute this rec1 should come only 7 times in whole file. 2.I need validating script for this....