Bash argument not expanding in script


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Bash argument not expanding in script
# 1  
Old 10-11-2019
Bash argument not expanding in script

I pass an argument to bash as run. The first command in green executes as expected, however the second in blue fails as the $run does not expand. I tried to escape the variable with \ thinking the quotes were making the literal translation and also "${run}" but both did not work to expand the variable run. What am I missing? Thank you Smilie.

Code:
bash script <run>

Code:
run=$1

printf -v cmd_q '(cd path/to/dir/%q*/*/out && exec sshpass -f out.txt scp -- *.txt* user@xxx.xx.xxx.xx:/path/to/dentination/*/folder)\n' "${array[@]}"
sshpass -f file.txt ssh -o strictHostKeyChecking=no -t xxx@xxx.xxx.xxx "$cmd_q"

printf -v cmd_q '(cd path/to/dir/%q*/*/out && exec sshpass -f out.txt scp -- *.txt* user@xxx.xx.xxx.xx:/path/to/dentination/"$run"/folder)\n' "${array[@]}"
sshpass -f file.txt ssh -o strictHostKeyChecking=no -t xxx@xxx.xxx.xxx "$cmd_q"

# 2  
Old 10-11-2019
Try export run=$1 to make the variable visible in child processes. I can't see a mistake in the syntax.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 10-13-2019
Shells don't expand variables within single quotes.
These 2 Users Gave Thanks to RudiC For This Post:
# 4  
Old 10-14-2019
Thank you very much, the below worked Smilie

Code:
run=$1
printf -v cmd_q '(cd path/to/dir/%q*/*/out && exec sshpass -f out.txt scp -- *.txt* user@xxx.xx.xxx.xx:'"$run"'/folder)\n' "${array[@]}"
sshpass -f file.txt ssh -o strictHostKeyChecking=no -t xxx@xxx.xxx.xxx "$cmd_q"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

[BASH] eval command not expanding variables as expected.

Hi Guys, I wrote a collection of bash functions years ago and now need to use them again but I'm getting some error messages when eval tries to expand the variables names. I recollect that I used the shopt command to set one of the options but I can't quite remember the command that I... (8 Replies)
Discussion started by: ASGR
8 Replies

2. Shell Programming and Scripting

checking first argument for tag in bash script

I have a bash script where I pass an argument ./chris.bash "\argv Test" I want to detect if the user supplied \argv at the start of the argument (3 Replies)
Discussion started by: kristinu
3 Replies

3. Shell Programming and Scripting

awk issue expanding variables in ksh script

Hi Guys, I have an issue with awk and variables. I have trawled the internet and forums but can't seem to get the exactt syntax I need. I have tried using awk -v and all sorts of variations but I have hit a brick wall. I have spent a full day on this and am just going round in circles. ... (3 Replies)
Discussion started by: gazza-o
3 Replies

4. Shell Programming and Scripting

bash script argument not outputing line -NULL

Hi everybody! I am writing a script which accepts two arguments one of them being a message. All is working except for the message part. That is, to accept text as an argument. I have trouble storing and echoing a line of text. It seems that writing a few words and store them in an... (6 Replies)
Discussion started by: bluetxxth
6 Replies

5. Shell Programming and Scripting

Using GET, passing argument to bash

Hi guys! So, I use GET ( Simple user agent using LWP library. ) on a remote text file that is then passed to bash and executed. However, I need to pass that bash script a single argument, and so far nothing that I have tried has worked, although I have learned quite a bit about input/output... (5 Replies)
Discussion started by: Rhije
5 Replies

6. UNIX for Dummies Questions & Answers

Expanding variables with Ed Bash 3.2.33

Hi, The following code finds the line containing fruits in test.txt and replaces instances of apple with banana. ed -s test.txt <<< $'/fruits/s/apple/banana/g\nw' What I want to do is put variables in the place of fruits, apple and banana. I have tried replacing ' with " to get... (2 Replies)
Discussion started by: de_la_espada
2 Replies

7. Shell Programming and Scripting

get positive number n as argument script must calculate the factorial of its argument

Can someone please help me with this SHELL script? I need to create a script that gets a positive number n as an argument. The script must calculate the factorial of its argument. In other words, it must calculate n!=1x2x3x...xn. Note that 0!=1. Here is a start but I have no clue how to... (3 Replies)
Discussion started by: I-1
3 Replies

8. UNIX for Dummies Questions & Answers

Bash script argument problem

I'm having problems with bash scripts. If a bash script is called with no arguments, I always get "PHIST=!" as the first argument (i.e. this is what $1 equals). Why? Where does this come from, and how can I fix it? Nothing in the bash man pages refer to this mysterious default argument. (2 Replies)
Discussion started by: sszd
2 Replies

9. Shell Programming and Scripting

check if argument is an ip address in bash/sh

Hi all, Can you please suggest a few lines of if statement to check if a variable is an ip address purely in bash/sh? Thanks, Marc (3 Replies)
Discussion started by: marcpascual
3 Replies

10. Shell Programming and Scripting

expanding dotted paths to absolute ones in bash or sh

I have a little script to help me manage a gallery of image files. It makes symbolic links to every file in and below the current directory, placing them in a target directory which is passed to the script as a parameter. Unfortunately, the script pukes when I pass a parameter that contains... (4 Replies)
Discussion started by: TanRanger
4 Replies
Login or Register to Ask a Question