A variable is a name, if correctly chosen with a symbolic meaning, that holds a value or values. Use this tag if your question is specific on the use of variables on shell scripting (if you want to ask about variables in programming languages you should probably ask on StackOverflow)

learn more… | top users | synonyms

0
votes
2answers
29 views

Preform operation in bash only if a variable is less than a second variable

I am building an OS for a virtual machine I made, and while progress has been coming along swimmingly, I have been stuck for a good several hours on what should be a very simple issue: I am designing ...
6
votes
2answers
380 views

Why does changing $PATH affect child shells, but changing $foo not?

$ unset foo $ unset bar $ echo $foo $ echo $bar $ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games $ foo=a $ bar=b $ export bar $ echo $foo a $ ...
0
votes
2answers
27 views

Removing space at the beginning of the variable data

I would like to ask if how can I remove the space in the beginning of the stored data of the variable. I had an auto-generated script that will get MySQL COUNT from the DB. MYSQL COUNT SCRIPT-> .ini ...
1
vote
1answer
63 views

$($string) execution failure

I have $(${StrVar}), my StrVar has $ in value eg. test$9hp. When I execute the command: $(${StrVar} it fails as $9 is inside my variable and it tries to expand it. function Deploy { # in this ...
0
votes
2answers
47 views

Define a set of values for a variable in shell script

I want to define a set of values for a single variable. E.g. singleDigit={0,1,2,3,4,5,6,7,8,9} If the user gives 10 it should exit with an error. How can this be achieved without using a loop, but ...
3
votes
3answers
151 views

Could not execute a Bash Script if / while

I would like if someone points out mistakes in my script. The source where I'm learning from is so buggy that's why it's confusing me. PURPOSE OF THIS SCRIPT: It will count the numbers from ...
1
vote
0answers
29 views

Save return value from a function in one of its own parameters

Function: Func1() { str1=$2 $1=$str1 } Call function: local var1 Func1 var1 "Helle World" echo $var1 I know this will work but I guess it's not very "pretty": Func1() { str1=$2 eval ...
1
vote
1answer
37 views

Output in variable shouldn't be interpreted as command [duplicate]

So this question was asked some times already, but the answers didn't seem to work for me. So I have this simple script #!/bin/bash charon_id = $(pidof charon) kill -1 $charon_id And console says ...
0
votes
1answer
24 views

Transferring a variable from one file to another

I have two files, config and template, and I want replace parts of the template with variables from config: config.txt contains: MYURL='www.google.com' template.txt contains: url = MYURL I used ...
0
votes
0answers
52 views

copying a file via a linux bash script: seemingly not working

Below is a simple script I have written to collect data contents of files from different directories named dyn1, dyn2, dyn3 ... dyn15. I will add more to the array list (other file names) but for now ...
0
votes
1answer
38 views

Assign hostname to a variable in ksh

I am trying to assign the output of hostname to a variable and use it in my script. While I was able to get the variable to another logic, when I try to print the variable it doesn't print the ...
3
votes
1answer
25 views

Loop over a string in zsh and Bash

I would like to convert this Bash loop: x="one two three" for i in ${x} do echo ${i} done in such a way to work with both Bash and zsh This solution works: x=( one two three ) for i in ${x[...
12
votes
1answer
236 views

See the expansion of Bash variables for debugging?

Is there any tools/tricks available for expanding variables to make bash code easier to read? For example it would convert the following: DIR=/var/tmp FILE=${DIR}/file SCRIPT_ROOT=/opt/root TOOL=${...
0
votes
1answer
43 views

script set command output to variable [duplicate]

I'm writing a simple script that prompts the user for information and saves it as a variable to be passed as arguments in another command. One of the variables stores a path which may include a space. ...
-1
votes
1answer
44 views

for loop # usage

I am trying to understand how this piece of code works: for b in `git branch -r`; do git branch --track ${b##upstream/} $b; done In particular, the part where it does ${b##upstream/} I know it ...
3
votes
4answers
138 views

Assigning command to variable without alias

I just accidentally found out that in bash, we can assign a command into a variable without using alias. g=date $g Mon Jun 27 13:00:40 MYT 2016 That works. Here's another example: jj="ping yahoo....
-2
votes
2answers
59 views

How to compare “abc” with abc in a shell script?

I did not find the answer other location, so seek help here. My code need compare "stop" with stop this is stand bash string. pi@raspberrypi:~/Voice $ ./test.sh | more "stop" stop My code: #!/bin/...
0
votes
5answers
56 views

Return one value and save it into a variable

I have this sentence that returns two values. cat /ora$sid/dbs/listener.ora|grep PORT|sed 's/.*PORT.*\([0-9]\{4\}\)\(.*\)/\1/' Return 1521 1522 With this sentence I want to get only one value. ...
2
votes
1answer
34 views

shell script echo text name in for looping multiple texts

I am trying to do this text1="word1 word2 word3" text2="word4 word5" text1="word6 word7 word8" for var in $text1 $text2 $text3 do echo $var" in "(__?__) done expected output word1 in text1 ...
2
votes
2answers
97 views

BASH: Using awk to filter unique lines results in 0 length array

Note: Thank you to Jeff Schaller and steeldriver. But as neither posted as an answer, I'm not sure how to mark as solved. I now have a better understanding of pipes/subshells. I'm pretty sure I once ...
15
votes
1answer
758 views

Why does awk print the whole line when I want it to print a variable?

When I type: echo "foo bar" | gawk -v var="content" '{print $var}' I expect the output to be content. Why is it foo bar?
1
vote
2answers
48 views

Defer variable expansion to subshell

My service uses environment variables extensively for configuration (mostly because it's really easy to inject those into a Docker container). I also have some maintenance scripts that I can run ...
0
votes
0answers
20 views

Echo variables sshpass [duplicate]

I want to do sshpass to a server to get lots of information from it. But as some logic will be involved, I tried to create a variable inside sshpass and echo to the client (first test). It is not ...
1
vote
1answer
448 views

How to sed -i '`[echo $var]`i word' file?

Code: pos=2; printf "Masi \nwas \nhere" > /tmp/1 sed -i '`echo $pos`i huhu' /tmp/1 Expected output: Masi huhu was here Current output: sed: -e expression #1, char 1: unknown command: ``'
0
votes
0answers
18 views

Ansible variables scope clash

I've got an ansible setup that looks like this inventories/ sandbox1 group_vars/ all sandbox1 pb/ pb1/ run_pb1.yml I've got roles and other stuff too, but I think ...
0
votes
1answer
49 views

Difference between set variable “value” and set variable value?

What is the difference between the following 2 examples in a shell script: set variable1 "value" set variable2 value
1
vote
1answer
55 views

Redirect bash variable into netcat

I am attempting to write a bash loop that passes a bash string into netcat. The command that I execute is: nc -nv [ipaddress] [port] << EOF 10000 EOF I am writing a bash loop to perform this ...
1
vote
3answers
82 views

Creating a new file inside awk but in different directory based on input field value

I am trying to work with awk datetime=`date +\%Y\%m\%d` logdatetime=`date +\%Y\%m\%d` foldermonth=`date +\%B_\%Y` folderday=`date +\%d` inputdir="~/sboper/Standalone/fsplit/GLMS" ...
1
vote
1answer
46 views

Output broken when appending output of sql query in ksh

I'm very new to scripting. I would like to add the output of the sql function given below to another variable for example: month_end () { mdate=$($SQLPLUS_HOME/sqlplus.exe -S $DBC << END set ...
5
votes
3answers
496 views

Exclamation mark variable `$!`

I have been studying for my Unix exam and I often saw exclamation mark variable - $! If I write echo $! on my Mac shell tells me something like "You have mail in…" Is purpose of this to store whether ...
1
vote
3answers
57 views

Variable assignment syntax error in shell [closed]

VALUE=${VAR| |o} I found this variable assignment in shell script. I can't understand why || sign is used here. EDIT Code given in a comment: #!/bin/bash source /opt/cpf/bin/cpf_logging_helper.sh ...
2
votes
4answers
41 views

Why does “ $Namefile* ” give me such a result?

In my code , there is such a part ; Namefile=$1 for n in $Namefile* do echo $n done and just for this part's result is ; $ sh example.sh hw1 hw1_evening_sun.txt hw1_morning_sun.txt hw1_out_si_wire....
1
vote
3answers
70 views

Where are shell variables stored?

For example, if I make a parameter expansion: $ b=1 $ echo $b 1 Where is b stored internally? I checked and it is not an environment variable because it is not listed by printenv. I'm writing my ...
2
votes
4answers
60 views

Create text corresponding to multiple variable assignments from a single variable

I have a variable that has a value like this line=dog,/path1,/path2 What I want is to create a variable from the value; i.e dog should be the variable and want to get the below output: dog=/path1,/...
3
votes
2answers
357 views

Is it possible to make the variable inside nested for loop local

#!/bin/bash for i in 1 2 3 4 do # { # local i ##gives error local: can only be used in a function for i in 1 2 3 do echo "inner loop ----- $i" (( i++ )) done # } echo $...
1
vote
2answers
68 views

Scope of Local Variables in Shell Functions

After reading 24.2. Local Variables, I thought that declaring a variable var with the keyword local meant that var's value was only accessible within the block of code delimited by the curly braces of ...
3
votes
3answers
56 views

Create a nickname to substitute a string without $VARIABLE, in bash cli

Is it possible to create nicknames for frequently used strings, without using the '$' in front of $variables? The string can be fixed and does not need to change for the lifetime of the shell prompt. ...
1
vote
2answers
37 views

Scope of an exported variable in shell

Say I have a script where I have exported a variable as seen below: #!/bin/bash foo(){ eval export one=1 } foo1(){ eval export two=2 } (foo) foo1 echo "one=$one" echo "two=$two" However I ...
8
votes
2answers
384 views

How to make `local` capture the exit code?

In my project I have the following snippet: local output="$(bash "${1##*/}")" echo "$?" This always prints zero due to local, however, removing local causes the $? variable to behave correctly: ...
3
votes
2answers
77 views

How to export a variable from a perl script into a shell script?

I have a shell script in which I am running perl script by below code. perl perlscript.pl In the perl script I have defined a variable called $circle. Now I want to use this variable value in my ...
1
vote
2answers
31 views

Variable assignment in a ksh script

I am viewing a ksh script and I see a function where the variable has been defined as below. Can anyone explain what exactly the below assignment of variable means in ksh script? temprule="\$${...
1
vote
2answers
60 views

How to modify (not add to) a number variable?

Suppose that a=$((1)) How can I change the value of a from 1 to 01? I just want to insert the 0 before the current value of a and rewrite it. a=$((01)) would not be a solution in this case because ...
2
votes
1answer
70 views

Can I determine type of an awk variable?

I have the gawk version of awk. In this part of gawk manual, it is stated that awk variables have "attributes", which are used to determine how to treat them in various operations. For example, a ...
0
votes
2answers
40 views

When does an array variable represent the entire array, and when the element indexed by `0`?

Bash manual says: Referencing an array variable without a subscript is equivalent to referencing with a subscript of 0. In Bash or POSIX shell, is there some general rule for when an array ...
3
votes
3answers
70 views

set variable inside heredoc and use them outside

I'm trying to set the variable 'count' while running the command inside a heredoc together with timeout, but I'm unable to get the 'count' variable outside the heredoc. How can I achieve that? My ...
1
vote
1answer
40 views

copy array with array name inside string in bash

In bash 4.3 script, I have variables: environment="local" config_local=("a" "b" "b" "d") copy_from="config_${environment}" I want to copy values from array with name stored in copy_from to another ...
0
votes
3answers
50 views

BASH: pass arguments to sub-script

I have a script like this: #!/bin/bash . config ./sub-script There are a lot of variables in config and I don't want to pass them to sub-scirpt like ./subscript arg1 arg2 ... arg100500. Also, I ...
1
vote
1answer
54 views

BASH Variable checker function

I am struggling to figure out a way to reduce code duplication. Specifically when checking if a variable is set. My code currently looks like this: # webadmin enabled if [ -f "${servercfgfullpath}" ];...
3
votes
1answer
173 views

Variables of integer type in Bash?

There are two ways to define and use variables of integer type in Bash declare -i a new variable use a variable in an arithmetic expression, without declaring it. My questions: What are the ...
0
votes
4answers
62 views

Setting variables from shell: how to use them in a script?

How can I set variables to be used in scripts as well? They don't have to be global / system-wide, just the current session is sufficient. But for some reason they seem gone even if I run a script ...