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)

0
votes
2answers
21 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
30 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
47 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
98 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
88 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
14 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
10 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}
0
votes
1answer
47 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
24 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
44 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
24 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
17 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
394 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
55 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
213 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
32 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
24 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
163 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
31 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
59 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
80 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
122 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
54 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
189 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
44 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
69 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
47 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
72 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
19 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
66 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
66 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
643 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 & ...
2
votes
1answer
348 views

What is the purpose of BASH_ARGV0 in bash?

In the release for Bash-5.0 it is mentioned that they added BASH_ARGV0: a new variable that expands to $0 and sets $0 on assignment. What is the purpose of this? When is this an improvement over ...
0
votes
1answer
78 views

awk is not working for my requirement [duplicate]

My requirement is that shell script should print the folder name of the file. For example comp.class is present in D:/Practice/HelloWorldPractice/bin folder, I should get output after executing ...
1
vote
2answers
89 views

MQTT as a variable on Bash?

Hi I want to try and convert a message receieved from MQTT into a variable so I can do an "IF script" using messages sent from a certain topic on mqtt. I tried this at top of script but didn't work: ...
2
votes
1answer
19 views

Ambigious redirect error on different volumes with mysql

i want to import a database with mysql (running with MAMP): #! /bin/bash # variables currdir=$PWD dbname=TEST dbuser=root dbpass=root dbpath="/applications/MAMP/Library/bin" exname=database if [ "$1"...
0
votes
2answers
103 views

Finding the Percentage from a grep variable

I want to display the calculation of the CPU load in percentage. num2=$(uptime | grep "average:" | tr -d " " | cut -d ':' -f 5 | cut -d ',' -f 2) percent=$((num2(*100))) echo 'CPU percentage' $...
1
vote
1answer
56 views

Remove all lines that contain $PWD

I have a file that contains paths - looks like this: /Users/a/Desktop /Users/a/Documents /Users/a/Documents/Work What would be the easiest way to remove all lines that contain the current directory (...
5
votes
2answers
589 views

Subtract time using bash?

Is it possible to use bash to subtract variables containing 24-hour time? #!/bin/bash var1="23:30" # 11:30pm var2="20:00" # 08:00pm echo "$(expr $var1 - $var2)" Running it produces the following ...
4
votes
2answers
119 views

What's the purpose of adding a prefix on both sides of a shell variable comparison to a string literal?

I have encountered comparisions of variables to string literals multiple times over the years which had one character prefixing the variable and the literal, e.g. if [ "x$A" = "xtrue" ]; then in ...
0
votes
1answer
47 views

how to get several commands into one variable for ease of use

I am writing a script and I noticed that a certain line of code is constantly being reused. So I thought why not put it into a variable for ease of use, and when something changes, I only need to ...
10
votes
2answers
649 views

Why can I not use variables as prefix to a command to set environment variables?

Normally, it is possible to set an environment variable for a command by prefixing it like so: hello=hi bash -c 'echo $hello' I also know that we can use a variable to substitute any part of a ...
0
votes
0answers
34 views

Complex nested for loop in directory

I have two sets of files in two separate directories organized as such: #first directory /direc1/files file0.num1.tar.gz file0.num2.tar.gz file0.num3.tar.gz file0.num4.tar.gz . . . ...