Pass in Bash Terminal Variables to a Bash Script

Pass all variables from one shell script to another?

You have basically two options:

  1. Make the variable an environment variable (export TESTVARIABLE) before executing the 2nd script.
  2. Source the 2nd script, i.e. . test2.sh and it will run in the same shell. This would let you share more complex variables like arrays easily, but also means that the other script could modify variables in the source shell.

UPDATE:

To use export to set an environment variable, you can either use an existing variable:

A=10
# ...
export A

This ought to work in both bash and sh. bash also allows it to be combined like so:

export A=10

This also works in my sh (which happens to be bash, you can use echo $SHELL to check). But I don't believe that that's guaranteed to work in all sh, so best to play it safe and separate them.

Any variable you export in this way will be visible in scripts you execute, for example:

a.sh:

#!/bin/sh

MESSAGE="hello"
export MESSAGE
./b.sh

b.sh:

#!/bin/sh

echo "The message is: $MESSAGE"

Then:

$ ./a.sh
The message is: hello

The fact that these are both shell scripts is also just incidental. Environment variables can be passed to any process you execute, for example if we used python instead it might look like:

a.sh:

#!/bin/sh

MESSAGE="hello"
export MESSAGE
./b.py

b.py:

#!/usr/bin/python

import os

print 'The message is:', os.environ['MESSAGE']

Sourcing:

Instead we could source like this:

a.sh:

#!/bin/sh

MESSAGE="hello"

. ./b.sh

b.sh:

#!/bin/sh

echo "The message is: $MESSAGE"

Then:

$ ./a.sh
The message is: hello

This more or less "imports" the contents of b.sh directly and executes it in the same shell. Notice that we didn't have to export the variable to access it. This implicitly shares all the variables you have, as well as allows the other script to add/delete/modify variables in the shell. Of course, in this model both your scripts should be the same language (sh or bash). To give an example how we could pass messages back and forth:

a.sh:

#!/bin/sh

MESSAGE="hello"

. ./b.sh

echo "[A] The message is: $MESSAGE"

b.sh:

#!/bin/sh

echo "[B] The message is: $MESSAGE"

MESSAGE="goodbye"

Then:

$ ./a.sh
[B] The message is: hello
[A] The message is: goodbye

This works equally well in bash. It also makes it easy to share more complex data which you could not express as an environment variable (at least without some heavy lifting on your part), like arrays or associative arrays.

Fetch variables values from yml and pass to shell script?

Read name value pairs from yq and use bash printf -v to initialize variables.
A little over the top, but a nice way to initialize a script.
Note that adding variables does not change the while loop.

file.sh

#!/bin/bash

while read -r key val; do
printf -v "$key" "$val"
done < <(yq -r '.variables | keys[] as $k | "\($k) \(.[$k])"' data.yml)

echo "Details are $name: $pass - Total value $count"
echo 'Some extra variables:'
echo "\$address: $address, \$phone: $phone, \$url: $url"

data.yml

variables:
count: "100"
name: "sss"
pass: "123"
address: "42 Terrapin Station"
phone: "999-999-9999"
url: "http://www.examle.com"

output


Details are sss: 123 - Total value 100
Some extra variables:
$address: 42 Terrapin Station, $phone: 999-999-9999, $url: http://www.examle.com

How to pass shell variables as Command Line Argument to a shell script

Bash scripts take arguments after the call of the script not before so you need to call the script like this:

./StatCollection_DBServer.sh DD 50

inside the script, you can access the variables as $1 and $2 so the script could look like this:

#!/bin/bash
LOG_DIRECTORY="${1}_${2}users"
mkdir -m 777 "${LOG_DIRECTORY}"

I hope this helps...

Edit:
Just a small explanation, what happened in your approach:

prodName='DD' users=50 ./StatCollection_DBServer.sh

In this case, you set the environment variables prodName and users before calling the script. That is why you were able to use these variables inside your code.

How to pass variables in a shell script as arguments?

To access the value of any variable in shell script, you can do it using the $ sign.

So below is how you can do it :

#Setting variables to pass into args
country=USA
month=10

python A.py --month="$month"
python B.py --country="$country" --month="$month"

How do I pass a variable (string) to a bash script from python and later echo it?

Bash file:

#!/bin/sh

echo "$1"

Python file:

import subprocess

path_to_bash_file = 'path_to_bash_file'

arg_1 = 10

subprocess.check_call([path_to_bash_file, str(arg_1)])

Variables as commands in Bash scripts

Simply don't put whole commands in variables. You'll get into a lot of trouble trying to recover quoted arguments.

Also:

  1. Avoid using all-capitals variable names in scripts. It is an easy way to shoot yourself in the foot.
  2. Don't use backquotes. Use $(...) instead; it nests better.


#! /bin/bash

if [ $# -ne 2 ]
then
echo "Usage: $(basename $0) DIRECTORY BACKUP_DIRECTORY"
exit 1
fi

directory=$1
backup_directory=$2
current_date=$(date +%Y-%m-%dT%H-%M-%S)
backup_file="${backup_directory}/${current_date}.backup"

tar cv "$directory" | openssl des3 -salt | split -b 1024m - "$backup_file"


Related Topics



Leave a reply



Submit