1

I have a shell script in which I am passing the same value many times as arguments. Instead of hardcoding this redundant value across all py scripts, I wanted to see if I can work with variables and pass those into the py script as arguments.

Here is the script.sh:

python A.py     --month=10
python B.py     --country=USA --month=10

I would like something like this:

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

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

How would I do this?

tripleee
  • 175,061
  • 34
  • 275
  • 318
cesarteaser
  • 111
  • 1
  • 8

3 Answers3

0

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"
Prashant Kumar
  • 2,057
  • 2
  • 9
  • 22
  • 1
    Needs quotes to behave reliably. `"$country"`, not just `$country`. Run the code in this answer through http://shellcheck.net and follow the links in the warnings it emits. – Charles Duffy Jul 01 '20 at 03:58
  • Thanks for pointing this @CharlesDuffy. I was going to include this in the answer. But then `quotes` are used to preserve the whitespaces in the variable. In the above example we don't have any whitespace. But anyways it is better to include for precautionary purpose. – Prashant Kumar Jul 01 '20 at 04:07
  • If it would always be the same value given in the question's example, there would be no reason to use a variable at all. Anyhow, the point of Stack Overflow is to teach, and teaching a practice that has bugs when the values have unexpected comments is a bad idea for obvious reasons. – Charles Duffy Jul 01 '20 at 13:47
  • Even if the values in above example were kept to be same, OP said he want to use these values at multiple places in the script. It would be good to have it in a variable so that in case any change is needed to value, he can do it at just one place. Anyways, I accepted the fact that it would be nice to include the variable in quotes for precautionary measures. Thanks again. – Prashant Kumar Jul 01 '20 at 18:03
0

If you have to use arguments passed to script, try something like below

script SomeMonth SomeCountry

Then in script

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

The positional parameters..

$0 = script (script Name)

$1 = SomeMonth (first argument)

$2 = SomeCountry (Second argument)

And so on..

Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34
  • 1
    This too needs quoting; see [When to wrap quotes around a shell variable?](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable) – tripleee Jul 01 '20 at 04:58
  • 1
    Not only that, it is always unsafe to not quote unless you have complete control over the variable's contents and know that it doesn't contain whitespace or other shell metacharacters. – tripleee Jul 01 '20 at 06:14
-3

Just to add something to the other correct answers. If you use those arguments many times, you can also embed the argument name together with its value, and save typing and errors:

# Setting variables to pass into args
p_country="--country=USA"
p_month="--month=10"

python A.py     $p_month
python B.py     $p_country $p_month

Note 1: beware of values containing spaces, or any other "strange" characters (meaningful to the shell). Ordinary words or numbers, like "USA" and "10" pose no problems.

Note 2: you can also store multiple "arg=value" in a single variable; it can save even more typing. For example:

  p_m_and_c="--country=USA --month=10"
  ...
  python B.py     $p_m_and_c
  • This is not good practice; it will break in confusing ways if your variables contain whitespace or other shell metacharacters. You can fix that by using arrays instead of traditional scalars, but then that breaks portability to traditional Bourne/POSIX shell. – tripleee Jul 01 '20 at 04:57
  • @tripleee I assumed the arguments are set as literal in the script, like the example the OP posted, and I put Note 1 to warn about that. I'll update the Note 1. – linuxfan says Reinstate Monica Jul 01 '20 at 05:00
  • @tripleee did you read the question? Do you know why the OP wants to use variables? – linuxfan says Reinstate Monica Jul 01 '20 at 15:03
  • 1
    @linuxfansaysReinstateMonica, answers aren't just written for the one person who asked the question; they're written for everyone reading that question and trying to learn from its answers in the future. If something is going to be taught in a way that has limitations that make reuse dangerous, those limitations need to be clear and explicit (even for the OP, since a practice they're taught for use in scenario-A is something they're likely to try to use later in scenario-B). – Charles Duffy Jul 01 '20 at 18:14
  • @linuxfansaysReinstateMonica, ...indeed, a big part of why the bash community built the [Wooledge BashGuide](https://mywiki.wooledge.org/BashGuide) in the first place was the TLDP ABS showcasing bad-practice examples that didn't exhibit actual bugs if narrowly interpreted, but led to people writing buggy code when trying to use them in other scenarios; those of us in the #bash IRC channel on freenode got fed up with people coming in with bugs they got following that "documentation"'s examples outside the narrow scope those docs were written to be correct in. – Charles Duffy Jul 01 '20 at 18:17
  • Well @CharlesDuffy, I believe that Note 1 is clear enough, but it is possible to make it bold or extend it (by me or someone else). Is the problem the absence of quoting around $p_month? Or the problem is embedding the name of the switch together with "=" and the value? I understand your point but, now, I would tell the OP to avoid shell as hell and use python or pascal or C instead - they do not have so many caveats. And you have to type a lot more. – linuxfan says Reinstate Monica Jul 02 '20 at 06:15