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
25 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
38 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
1answer
29 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
42 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
25 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
40 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
47 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
146 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
44 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 ...
-1
votes
0answers
35 views

How can I print the result of a tr command through a variable

I am using the command below: echo "$file1" | tr "\n" "," It is printing results like this: rose,lotus,jasmine,marigold If I assign the result to a variable it is printing output like this: $r ...
0
votes
1answer
19 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
34 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
48 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
28 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
68 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
24 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
26 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
31 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
56 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
30 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
56 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
34 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
30 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
43 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
30 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
72 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
14 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
25 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
99 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
45 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
284 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
47 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
576 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
58 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
62 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
470 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 ...
0
votes
1answer
34 views

Script with variable in path

I'm not entirely sure where to ask or how to formulate this question with correct terminology. I have script here for managing a game (minecraft) server for the user (bukkit). I've cut out some ...
1
vote
1answer
38 views

Assign path to variable which does not exist

In an effort of keeping things DRY, I am trying to assign a path to a variable that may not exist and have the following script which returns: /apps/tmp: No such file or directory #!/bin/bash ...
3
votes
5answers
98 views

Bash - Store Color Codes in Variable

How do I store colored text in a variable and print it with color later? I never see the answer to this question in 100 searches it's all about PS1 prompts, or inline printf's or using data from ls ...
0
votes
1answer
52 views

Where is my .sh script syntax error(s)?

Trying to run a small simple script to pull the java -version information from an AIX system's several java64 folders piped into an output/log file using the system's hostname but can't seem to get it ...
1
vote
1answer
41 views

Relation between shell start up processes and environment of parent process

"How are shell variables related to the environment that a process started by the shell has on start-up?" I read If processes inherit the parent's environment, why do we need export? My current ...
1
vote
1answer
81 views

Inserting variables in a command within a shell script

I'm attempting to write an automated script which will update the IP address for my Raspberry Pi within my /etc/hosts file. I'm able to execute this line just fine, IP=`sudo nmap -sP 10.61.4.0/24 | ...
1
vote
1answer
49 views

Linux bash script input without using “read” [closed]

I need to create a bash script where a single argument is passed by the user (without using the "read" input option) at the terminal command line that creates a directory with that name, or otherwise ...
0
votes
3answers
182 views

Passing Variable from one script to another

How can I pass a variable between scripts? Example: I have two test scripts: -- test2.sh #!/bin/bash echo "this is test2.sh" echo "varname is $varname" echo end -- tes1.sh #!/bin/bash ...