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

1
vote
3answers
63 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
54 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: ...
3
votes
2answers
332 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
38 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 ...
2
votes
3answers
39 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
30 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 ...
7
votes
2answers
366 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
49 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
26 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? ...
1
vote
2answers
59 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
51 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
39 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
46 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
30 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
44 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
50 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
153 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
46 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 ...
0
votes
1answer
22 views

How do you handle multiple validation rules on varied user input?

I'm using a Bash script to prompt users for several variables before continuing. If I want to create static validation rules and execute them on user inputs in an "a la carte" fashion, how would I go ...
1
vote
1answer
38 views

how to get or reflect the name of the bash function which is called? [duplicate]

i did not yet found a solution to this. Anyone a hint? i sometimes write bash functions in my shell scripts and i love to have my scripts being verbose, not just for debugging. so sometimes i would ...
1
vote
2answers
51 views

How to sum the output of two commands and save it to a file?

I have two kfsql commands that get total count of users. I need these two command outputs to be added up and printed to a file.txt I tried: var1= getdatabase1 var2= getdatabase2 echo "$var1" echo ...
1
vote
1answer
29 views

Bash - Catch input for a substituted command stored in variable

How would I be able to catch the input for a substituted command stored in a variable? Example: How would I be able to catch the input for the following command? bar_foo=$(openssl passwd -1 -salt ...
0
votes
3answers
100 views

Why is the array empty after the while loop? [duplicate]

I try to initialize an array in bash-4.2 next way: ring=() ls -las | tail -n +4 | while read line > do > ring+=("$line") > echo ${ring[-1]} > done 3924 -rw-r--r-- 1 username group 4015716 ...
1
vote
1answer
26 views

Which shells have functions where “local” does not alter exported variables for child processes?

In the example below, an exported variable is re-set as local in a function. Bash, Zsh, Fish don't pass on the original value to the child process. Are there any shells that make local affect the ...
1
vote
1answer
31 views

Error with find when using -newerct values stored in variables

I'm trying to create a bash function that will use a simple file such as sample.txt start=22 Mar 2016 10:00 end=22 Mar 2016 12:09 ...and find files that I have installed to /usr/local within the ...
3
votes
2answers
37 views

How is the return status of a variable assignment determined?

I have seen constructs in scripts such as this: if somevar="$(somecommand 2>/dev/null)"; then ... fi Is this documented somewhere? How is the return status of a variable determined and how does ...
0
votes
3answers
33 views

How to get help for bash variables use from a command line?

Bash has some special or predefined variables, such as $? and $#. How to know the meaning of those variables use build in command instead of searching online web pages? I have try to use this: ...
0
votes
1answer
15 views

How can I get a variables datatype in zsh?

For example zsh uses the variable $fpath to define include directories. How can I tell what type of variable $fpath is? i.e. is fpath a string number numeric array associative array
-1
votes
2answers
61 views

Finding the number of files in a folder [duplicate]

How can I find a number of files in a folder, assign it to a variable, then echo that variable, all in one command line?
0
votes
1answer
32 views

how can we change the DISPLAY variable in linux [closed]

What is the command to change the DISPLAY display variable in Linux, because while installation of a program I am getting the error as in the attached screen shot, so please help me how to get it out ...
3
votes
3answers
63 views

bash array with variable in the name

I'll appreciate your help with the following issue: I'm trying to set an array which contains a variable as part of the array name, example: Arr_$COUNTER (where $COUNTER is changed based on a loop ...
1
vote
0answers
39 views

Evaluate variables' value in Kickstart file

I am trying to make a Kickstart install of a CentOS 7 VM interactive, by asking for user input in the %pre-install script, as described here. So far I've managed to get user-entered values and store ...
0
votes
1answer
20 views

How to define a shell variable as equal to the version of Vim presently installed?

I am intending to write a shell script (if specifics are required, it will be executed in Zsh) that will determine the latest Vim patch via running: git pull origin master vernew=$(git describe ...
1
vote
1answer
33 views

Scope of variables in a process substitution

I have a command which takes two arguments. Assume we want to create both arguments by process substitution: cat <(var=123; echo $var) <(echo $var) What has to be done, to make $var available ...
0
votes
2answers
44 views

Which location does shell assign normal variable?

As you know, bash shell itself is also process. If I use command export a=c, then shell dynamically allocates variable a into location where environment variables are reside.(This environment section ...
0
votes
2answers
31 views

echoing a variable inside a variable

In my environment, the following variable is set: X=/home so echo $X gives me /home Now I set Y=$X echo $Y gives me $X. If I want to get /home using Y is it possible?
5
votes
2answers
93 views

How to store a path built with wildcards and containing with spaces into a variable

Here's the situation (I'm on a Mac, OS X El Capitan): # This works: $ cd /Applications/Adobe\ Illustrator*/Cool\ Extras.localized/en_US/Templates/; # These do not work: $ ...
0
votes
0answers
17 views

Export variable failed when I was compiling NETCDF

I'm compiling NETCDF on Linux. After configure, make,make install, I have already install NETCDF on /disk3/lly/lib/netcdf-4.1.3. Then, using vim ~/.bashrc,edit: export ...
1
vote
0answers
29 views

How do you pass variables to jsc/javascript?

I have a few javascript things that I am running from the Terminal by passing them to jsc, with <<<. (javascript is an alias to ...
1
vote
2answers
37 views

Running Emacs through the value of the bash variable

How would I add a value to a new variable in bash, so that when I echo the variable Emacs would run?
5
votes
2answers
104 views

In bash script, what's the different between declare and a normal variable?

In bash scripting: we create variable just name it: abc=ok or we can use declare declare abc=ok what's the different ? and why bash make so many ways to create variable?
0
votes
1answer
41 views

Create command line variables with PATH

As I am more of a web guy, i need a little help with this one, so I will explain what I am trying to do before I ask. I am creating batch script that will: GET request from an external server (json ...
0
votes
1answer
50 views

How to set up a pre-defined variable with multiple directory paths to use with the find command

I am looking to have a script delete files that are older than 90 days in certain directories. As in, there are 8 directories that have different paths to them. I can run the find command on one ...
8
votes
4answers
313 views

dirname and basename vs parameter expansion

Is there any objective reason to prefer one form to the other? Performance, reliability, portability? filename=/some/long/path/to/a_file parentdir_v1="${filename%/*}" parentdir_v2="$(dirname ...
1
vote
1answer
49 views

Use sed to retrieve variable from 1 file and place it in another

I need to use sed to retrieve a variable form within a file name. I am updating a previous code: cat /directory/filename1.xml | sed -e "s/XYZABC/${c}/g" > /directory/filename2.xml The value for ${c} ...
16
votes
6answers
579 views

Should variables be quoted when executed?

The general rule in shell scripting is that variables should always be quoted unless there is a compelling reason not to. For more details than you probably want to know, have a look at this great ...
0
votes
1answer
69 views

How do I write a Bash script that will ask users to define a variable?

I am presently modifying this shell script to turn it into a shell script for installing Arch Linux, with custom username(s), password(s), etc. At the moment I am wondering what I need to do in order ...
2
votes
2answers
74 views

Exported variables inside ssh session are empty

I am trying to export a variable inside a ssh session and reference the variable in the next write command. sshpass -p "password" ssh -t -t my-box <<EOF export newUrl="this is a url" sudo ...
2
votes
5answers
42 views

Help on edit a sed statement

I'm writing a script to change on the fly the firefox home page through a text variable: sed -i 's|\("browser.startup.homepage",\) "\(.*\)"|\1 "$ddrs"|' /home/max/.mozilla/firefox/*.default/prefs.js ...
3
votes
4answers
481 views

Sum of all Integers between two numbers

I would like to type in two numbers and get the sum of all integers between those two numbers, inclusive. For example: ./sum1 2 5 should yield sum: 14 because 2+3+4+5 = 14 Here's what I have ...