Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [variable]

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)

1
vote
2answers
39 views

grep from list of keywords

Over the past couple of days I've been trying to dig through directories of files to report on files containing key words. Through the help of other users showing me the correct syntax for grep and ...
1
vote
2answers
49 views

Bash array with folder paths and wildcards

Given the above folder paths, I am trying to write a script that will delete files older than a certain amount of days. I am trying to use some more advanced techniques than just hard coding the ...
-1
votes
1answer
45 views

Bash: I have a variable in bash script; [duplicate]

I have a variable in bash script dirs= "4"; I want to get this number from another file so I am using a command dirs= sed -n '1p' < test.txt; It gets me the number, I see it in the terminal. But ...
0
votes
2answers
44 views

What is a “name reference” variable-attribute? [duplicate]

After reading this answer by Kusalananda I got some sense about what is a variable-attribute, yet I miss what is a "name-reference" (type?) of a variable-attribute, what is it using for, why would one ...
2
votes
0answers
33 views

MediaWiki: How to totally prevent anonymous users from register? [migrated]

I've installed MediaWiki aiming to create some one-man-editing Wiki (anyone can read it). I went through the documentation but I found no variable that controls whether anonymous users can create an ...
-1
votes
2answers
49 views

The need for $1 and $2 for comparison with an here-string

This is a follow up to this question; I don't know why but I keep misunderstanding the following code, although I try very hard to understand it: function read_and_verify { read -p "$1:" tmp1 ...
1
vote
1answer
21 views

How do I capture this mysql output message in a variable?

I have this line in my script: mysqlinsert=$(/Applications/MAMP/Library/bin/mysql --host=localhost -uroot -proot --password=notmyrealpassword << EOF $mysqlrun) Despite what it returns, the ...
0
votes
1answer
60 views

Why value of a is 1?

I'm learning shell scripting and I am writing a script which lists the file for which the user has "7" permission, i.e. "rwx". So far I've written this: for F in * do a= $(stat -c "%a" "$F") ...
0
votes
2answers
48 views

here-document basename conflicts?

I am using EOF to generate bash scripts that run the Rscripts. In the Rscript I used basename to specify the output file name. When I use EOF to generate a list of bash scripts, I could not get ...
2
votes
2answers
68 views

Setting variable output from timeout

I'm trying to automatically query a status via telnet (this isn't something I'm able to work around in this case). The idea is to grep for something and assign the result to a variable, to pass to a ...
0
votes
2answers
51 views

Linux ECHO output of a ECHO variable [duplicate]

I have a variable (var) which return value as VAR1 ($var has the value VAR1). There is an input file which has VAR1 defined (VAR1=ABCDEF) How can I use echo to get the value ABCDEF using $var?? I ...
0
votes
2answers
32 views

Finding exact string from variable in an awk commmand?

I'm trying to the use the following awk command to return a user from a file that lists all users on the system but only if the 8th field is empty and they match the user variable (it's being using in ...
0
votes
3answers
34 views

Concatenating all arguments of a function/removing all spaces from a variable

What's a good way to concatenate all arguments of a function? What I got at the moment is iterating over them and appending them consecutively: func(){ concat= for arg in "$@" do concat=$concat$...
0
votes
4answers
49 views

Bash: while loop over numbered variables

My environment can have a flexible number of variables defined with the following format: Var1Value=A Var2Value=B Var3Value=C I now would like to iterate over the variables as long as they exists. ...
3
votes
3answers
113 views

tee into different variables

From the bash code command1 | tee >(command2) | command3 I want to capture the output of command2 in var2 and the output of command3 in var3. command1 is I/O-bound and the other commands are ...
1
vote
1answer
164 views

Passing arguments from one script to another

Let's say I am in a Bash script and am sourcing a file. How do I pass variables to another script after I source the file like this. sed -i 's/ = /=/' $file source $file Let's say file contains ...
0
votes
0answers
17 views

Add locale in variable for command [duplicate]

I'm trying to call a command from a variable in a script with a specific locale, example : $ myYTDL="LANG=C.UTF-8 youtube-dl --get-filename" but each time I try and call it in the script, I get : $ ...
0
votes
1answer
11 views

Length variable in netcdf file

I am trying to know the length of a variable 'i' in a netcdf file '122009.nc'. This is my script so far: #!bin/bash var=ncdump -v i 122009.nc var_length=${#var}
1
vote
1answer
167 views

Shell Script: Decrypt the encrypted password and store it in variable

I have a file "credential.txt" which contains the password of mysql. I have another script which calls this text file and retrieves the password from it. For security purposes, I don't want to store ...
1
vote
1answer
37 views

Does one string contain another in fish shell?

I am working on my fish.config for using the fish shell. I am trying to compare strings using bash syntax but fish doesn't accept the syntax. There is clearly another way to do it. Any suggestions ...
0
votes
3answers
94 views

How to prevent parameter expansion around a variable I want to be resolved?

Edit: Important note before reading further: since Kusalananda's anwser, my question is now quite useless, since he pointed out a bug in another script I used. Now this bug is fixed, the below errors ...
2
votes
2answers
31 views

zsh: How to get value of variable based on a another variable?

I think a example would best explain what I need _v1="windows" _v2_windows="/mnt/d" _v2_osx="/Volumes/d" echo $_v2_`echo $_v1` I want to echo the value of _v2_windows but using _v1 to determine ...
0
votes
1answer
26 views

Printf format error for number

After some greping and seding I manage to get a value from a csv cell and assign it to a variable (DBR). Here come some odd findings echo $DBR echo "$DBR", printf "%f" $DBR output: 9.1 ,.1 : ...
4
votes
3answers
421 views

Indirectly access environment variable [duplicate]

Given I have in a bash script ev=USER How can I get the environment variable value for $USER using ev? Tried naively doing: echo ${"$"$ev} which results in bad substitution. I'd expect to get ...
0
votes
3answers
74 views

How to break a long string into multiple lines assigned to a variable in linux bash script

I am working towards writing a bash script that contains a variable with a long string value. When I split the string into multiple lines it is throwing error. How to split the string into multiple ...
3
votes
1answer
267 views

Process substitution inside a subshell to set a variable

I'm trying to run a script remotely and use its standard output to populate a variable. I'm doing this to avoid temporary files. Here's the pattern I'm trying: var=$(bash <(curl -fsSkL http://...
0
votes
1answer
43 views

why curl can't expand variable inside the loop, but echo can?

I need to include variable inside curl for fname in "assets/*.drx"; do echo $fname curl -F file=@"$fname" "http://someurl.com/" echo $fnamename done Result: assets/...
0
votes
0answers
25 views

change global environment variable for all processes

I have two programs (lets say terminal A and terminal B) which both use a global environment variable to see if the other one changed something. I added UPDATED=0 to the /etc/environment file. Now ...
36
votes
3answers
4k views

Why is opening a file faster than reading variable content?

In a bash script I need various values from /proc/ files. Until now I have dozens of lines grepping the files directly like that: grep -oP '^MemFree: *\K[0-9]+' /proc/meminfo In an effort to make ...
3
votes
1answer
190 views

How to use variables inside back-quotes

I have a bash script that accepts a parameter "$input" and (among other things) directs it into a program and collects the output. Currently my line is: OUTPUT=`./main.exe < $input` But I get ...
23
votes
5answers
2k views

Why use double quotes in a [[ ]] test?

Let's say we have 2 integers in a bash script: value1=5 value2=3 Then why do we need to use double quotes in case of a test ? For example: if [[ "$value1" -eq "$value2" ]] Why not just use the ...
0
votes
1answer
36 views

Set bash variables from XML tags

I have the following xml file <root> <deviceID>IO238470374109730497</devicdeID> <KeyValuePairs> <KeyValuePair> <BASE-URL>site.com</BASE-URL> &...
18
votes
1answer
1k views

Why does ${$#} return same result as $$ in the shell?

While trying to get last positional parameter set in /bin/dash , I've tried echo ${$#}. Surprisingly this did not result in an error, but into PID which is the same as $$ variable contents. Question, ...
0
votes
3answers
62 views

Printing a Variable Which Contains $ Sign

I have a bash script which contains awscli as well. I am trying to print a variable which is created in a for loop. The variable that I am trying to print contains $ sign because of for loop. I couldn'...
0
votes
1answer
134 views

python -c “print calculation” gives syntax error

I was doing some bash scripting and adding some python for float calculations like divide, deductions When I am executing this, all part is running fine but for some python part it is showing me an ...
0
votes
1answer
20 views

Linux - Bash - variable assignment with command substitution is missing the newlines [duplicate]

I am running into a bit of an issue / quirk here: [root@s06p5bcsmta001 elves]# TEST=$(cat -e sendlog) [root@s06p5bcsmta001 elves]# echo $TEST | wc -l 1 [root@s06p5bcsmta001 elves]# cat sendlog | wc -...
4
votes
4answers
133 views

How to allow only certain characters in a bash variable

I want to prompt the user to input a URL, but it can only contain A-Z, a-z, 0-9, &, ., /, =, _, -, :, and ?. So, for example: Enter URL: $ http://youtube.com/watch?v=1234df_AQ-x That URL is ...
6
votes
2answers
281 views

How do I specify chance when setting a variable to random item in array?

Currently I do something like this to set a variable to a random item in an array: array=("foo" "bar" "baz") var=${array[$RANDOM % ${#array[@]} ]} How would I do if I want to set $var to one of ...
0
votes
2answers
65 views

While Read Line - Syntax Question

I am using a While Read Line state to parse our Openstack Swift accounts. Below is some code from a statement I am using to find all data with a specific creation/modification date. First I run a ...
7
votes
1answer
191 views

Insert a user defined variable in an alias in Bash

I would like to add an alias to a command in my .bashrc file, as in this example: alias take_row="cat prova.csv_001.txt | awk -v nrow="${nrow}" '{if (NR==nrow) print}'" But I would like to specify ...
1
vote
1answer
60 views

How to assign value at run time in AWK command

I am using below command to remove line feeds with in field values.This sample commmand created for file having 3 attributes. awk -F\| '{while(NF<3 && getline p)$0=$0 p}1' My input filess ...
0
votes
2answers
70 views

How to preserve shell variables in script after changing user

How can I access same variables data after switching users? For example: artifact=archive.tar.gz workdir=/home/user echo "$workdir/$artifact" sudo -u otheruser bash echo "$workdir/$artifact" ...
0
votes
1answer
30 views

control-flowed variable in Bash

The web-application document-root for Debian-Apache is typically /var/www/html/ but for Arch-Apache it's typically /srv/http/. I wrote a distro-agnostic LAMP-establishment script and I need it to ...
0
votes
2answers
57 views

awk + how to export value in awk

I use the following awk line in order to capture the remote machine that using the port 2181 netstat -nape | awk '{if ($4 == "43.64.56.36:2181") print $4, $5 ,$6 ,$9;}' 43.64.56.36:2181 43.64.56.60:...
3
votes
2answers
76 views

A Unix AIX variable without a $ symbol?

I'm trying to debug an existing program and found an if condition without $ symbol prefixed to it. Values are: dt_val=1234 prev_dt_val=1234 If condition goes like: if [ dt_val -eq prev_dt_val ] ...
0
votes
1answer
20 views

Is this a bug in fsarchiver --version

I learned that in order to capture the output of a Unix command one should use something like RESULT=$(command in question) I want to capture the version of fsarchiver to the variable RESULT because ...
1
vote
1answer
96 views

Define variable using read from stdin of heredoc

I am trying to store Available and Total Memory into variables in a script file as follows, read -r Available Total <<EOT $(free -m | awk '/^Mem/{print $7; print $2;}') EOT $ echo $Total $ ...
0
votes
1answer
128 views

Bash: Error in assigning default value to a variable

Inside my bash script: This works: CWD="${1:-${PWD}}" But, if I replace it with: CWD="${1:=${PWD}}" I get the following error line #: $1: cannot assign in this way Why I can't assign to ${1}?
1
vote
0answers
22 views

Correct way to quote variables for use in Bash backticks? [duplicate]

I have a script which I want to go over 4 sets of files PURO*.xml, PURR*.xml, SALO*.xml and SALR*.xml. for j in PUR SAL do mask=("${j}O*.xml" "${j}R*.xml") for k in 0 1 do ...
9
votes
3answers
651 views

“Variabilize” the ampersand (background a process)

I want to know if there's a way to put the ampersand in a variable and still use it to send a process to the background. This works: BCKGRND=yes if [ "$BCKGRND" = "yes" ]; then sleep 5 & ...