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
5answers
46 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
2answers
83 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 ...
1
vote
2answers
40 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
18 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
145 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
15 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
39 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
34 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
74 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
42 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
488 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
51 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
39 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
68 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
56 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
345 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
55 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
47 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
33 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
375 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
57 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
28 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
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
60 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
55 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
32 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
45 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
53 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
159 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
53 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
23 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
41 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
56 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
154 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
28 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
36 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
34 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
16 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
67 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
35 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
69 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
43 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
34 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
47 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
32 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
113 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: $ INSTALL_DIR=/Applications/...