Skip to main content
The 2024 Developer Survey results are live! See the results

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)

Filter by
Sorted by
Tagged with
2 votes
1 answer
35 views

Where are Perl arrays @0, @9999 documented?

$ perl -wle 'my @388=0..2;' Can't use global @388 in "my" at -e line 1, near "my @388" Execution of -e aborted due to compilation errors. $ perl -wle '@388=0..2;' $ Where can I ...
Dan Jacobson's user avatar
0 votes
1 answer
74 views

How to put all function/command call output results to different corresponding vars: for stderr, for stdout, for string result and for return code?

I want to extend question How to store standard error in a variable and get general solution (bash function or functions) to extract from the called function/command together with standard error also ...
Anton Samokat's user avatar
-1 votes
1 answer
41 views

Variables concatenated with & are not defined in subsequent commands

I would like to know why the following shows an empty dialog box: testvar="hello" & zenity --info --text "${testvar}" While this works: testvar="hello"; zenity --...
robertspierre's user avatar
0 votes
2 answers
57 views

Bash scripting: When to use variable, when function?

basic, innocent question: In bash scripting, why ever using a function, if one can set a variable containing command substitution with the essence of the function - a certain command or set of ...
futurewave's user avatar
7 votes
3 answers
1k views

Do I have a cleaner way to assign a parametrized json string to a bash variable?

I'd like to prepare a test for a REST petstore server by the mean of curl. To test its add new pet api, I prepare a json string that holds the Pet to insert in a bash function that executes the curl: #...
Marc Le Bihan's user avatar
0 votes
0 answers
76 views

Why are my strings being truncated including and after an equals sign?

I was writing a shell script for inserting a string value through a Microsoft sqlcmd variable into a Microsoft SQL server database table and noticed some unexpected behavior. It appeared the trailing ...
Mark's user avatar
  • 101
7 votes
1 answer
589 views

printf - store formatted string output in a variable

Instead of the following printf '%s\t%s\t%s\t%s\n' 'index' 'podcast' 'website' 'YouTube' I want to store the printf output in a Results variable, how can I do this?
Porcupine's user avatar
  • 2,066
0 votes
2 answers
88 views

how to replace a string with a variable?

I am trying to write a script containing a loop that enters a series of directories containing the same file, to replace a text string in a file with a variable that matches the directory's name. The ...
Ella Brudner's user avatar
5 votes
2 answers
433 views

Issue expanding variable with multiple wildcards in bash shell script with mv / rename

I wrote a bash shell script for Linux to move files in a static folder according to parameters specified in a data file. I've reduced that script to a minimal reproducible example to demonstrate an ...
End Antisemitic Hate's user avatar
0 votes
1 answer
29 views

How to integrate multiple inline `if` variable checks into `configure` flag selection

I'm compiling Nginx from source on Debian 12 in Bash. I'd like to select the TLS vendor (e.g. LibreSSL, OpenSSL) via a preflight variable ($nginx_tls_vendor). An example configure command without the ...
Pete Cooper's user avatar
0 votes
1 answer
57 views

value of $VAR already contains backtick and/or single quote inside. How to handle it? How to properly pass $VAR to program? [duplicate]

$ bash --version GNU bash, versione 5.2.26(1)-release (x86_64-pc-linux-gnu) I don't know how to deal with $VAR when its value inside contains single quote (') and/or backtick (`). I'm in the need of ...
dAllARA's user avatar
  • 33
5 votes
1 answer
303 views

How can I pass variables to a file printed from outside a bash script?

Assuming I have a variable v="c d e f g" and a file with the content: line1 $v line3 How can I make a bash script print this file with the content of the variable in line 2 as if I was ...
nath's user avatar
  • 5,824
0 votes
1 answer
67 views

Bash brace expansion doesn't work in an export statement

Consider the following example: $ echo file_{a,b,c} file_a file_b file_c # brace expansion worked :) $ export VARIABLE=file_{a,b,c} $ echo $VARIABLE file_c # brace ...
Thomas Fritz's user avatar
1 vote
1 answer
100 views

Why does -n with unquoted variable containing empty string return true? [duplicate]

From man bash: -n string True if the length of string is non‐zero. Examples: # expected $ var=""; [ -n "$var" ]; echo $? 1 # unexpected? $ var=""; [ -n $var ]; echo ...
pmor's user avatar
  • 619
5 votes
2 answers
764 views

Is it while loop or the pipe causing global variable behaving unexpectedly

Could please someone explain the (from my POV) strange behavior of the COUNTER variable in the following code? #!/bin/bash COUNTER=0 function increment { ((COUNTER++)) } function report { ...
Jozef Chocholacek's user avatar
1 vote
0 answers
42 views

Why does `$PS1` sometimes include the bell, `\a`? [duplicate]

In Ubuntu 18.04 and 22.04 (WSL), I've noticed that $PS1 contains a "bell" character, \a: $ echo $PS1 \[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\...
AJM's user avatar
  • 195
-1 votes
1 answer
106 views

Is the following `readonly` use POSIX-ly correct?

I defined the following as read-only: readonly root_command='sudo -s' later used in my script as in: exec $root_command My question is, maybe I am slow or something, but I do not fully understand ...
Vlastimil Burián's user avatar
0 votes
1 answer
136 views

How to assign null to or unassign a Bash variable if parameter is set?

Variable USE_TLS is to be unset, null, or empty when TLS is not to be used, but set to 1 when TLS must be enforced. I require it to be enforced by default (i.e., set), but not if I specify another ...
Code Maverick's user avatar
0 votes
0 answers
48 views

I want to issue "sudo su - <user>" to create a login shell, and pass an environment variable into the new shell [duplicate]

Oracle linux 7.9. Shell is bash. I log in as user bob. From there I sudo to user sam and do my work. But I want to pass a variable into sam's new login shell. So for example, this is what I currently ...
Andrew Grady's user avatar
1 vote
0 answers
91 views

diff two multilines bash variables to list what lines aren't in the other

I've created a bash script that Lists the document IDs I've put in an Elastic index, in $liste_ids_lines: a004-événements-examen_individus_localisés_ou_non a020-les_points-leurs_indicateurs-...
Marc Le Bihan's user avatar
1 vote
2 answers
140 views

Redirect to a filename that will be a variable and contain a command substitution

I'm using nmap to scan a network and then create an XML file that contains the scanned information. I want that XML file to contain the date of the scan so that each file is unique and I can see the ...
johnmacward's user avatar
1 vote
1 answer
237 views

Change hostname with a variable with for loop

I am trying to change the "hostname --transient" to some computers with a for loop using a variable. I want it to collect the "hostname -f" from the server itself and, using the ...
henrolos's user avatar
0 votes
1 answer
115 views

Using awk -v with Shell variable works great and prints to terminal, but when redirect to a file only the 1st record prints to the file

I am reading a file with a matching pattern then getting the first five lines after the matching pattern. The patterns are not in proper numeric order, so I am using a numeric variable in the pattern ...
Joey Simon's user avatar
1 vote
1 answer
175 views

some_variable vs some-variable in zsh

Why is it that zsh seems to understand what to do with some_variable=1 command, but not some-variable=1 command? E.g.: $ fs_screen=1 mpv --fs-screen=${fs_screen} someFile.mp4 works fine but $ fs-...
Harv's user avatar
  • 2,186
0 votes
1 answer
318 views

bash: string variable contains asterisk. how to use this variable for searching etc with grep, sed?

Have a nice day I have got text file (zz.txt): Chemical name 3-Aminopropane-1-sulphonic acid Synonym(s) -- Homotaurine * Tramiprosate -- Chemical name Common name and synonyms ... I have variable s=&...
Alex Den's user avatar
0 votes
1 answer
51 views

Bash script variable syntax: with some commands it works, with others it does not [duplicate]

I do not understand the behaviour of this variable: SSH_CONFIG_FILE="~/.ssh/config" echo $SSH_CONFIG_FILE ls -l $SSH_CONFIG_FILE ls -l ~/.ssh/config This is the output: ~/.ssh/config ls: ...
Pietro's user avatar
  • 573
1 vote
3 answers
63 views

sed: update 2 similar variables in a file but keep the upper and lowercase

I have 2 variables in a file like below that I need to assign a new value inserted by one user input: read -p "Enter CName Name : " CName sid=C02SBX SID=C02SBX When I run the following ...
Ali's user avatar
  • 123
0 votes
2 answers
337 views

Use SED to replace part of a current variable with user input variable

I'm trying to replace only part of the existing variable with a new user input variable as below: #Current Variable: gdbName=Test.MY.DOMAIN.COM <--I need to replace the "Test" (This can ...
Ali's user avatar
  • 123
1 vote
0 answers
252 views

Extract from a curl its dire failure message (if case of return code != 0), status code, output, and error message into four different variables

I need to extract from a curl emitted: the bash error message coming with a non-zero ?$ return code, in case my command would direly fail the http status code returned by the server contacted the ...
Marc Le Bihan's user avatar
4 votes
2 answers
1k views

How do I reliably capture the output of `ls` in this script?

The following script, play_movie.sh is meant to automatically select a movie if there is only one in the current directory and play it with ffplay. Otherwise it is meant to display a selection of ...
John Smith's user avatar
0 votes
2 answers
273 views

How to grep for the value of a variable inside a shell script?

I have 2 files: a file full of values I want to look for my source text file I wrote a short shell command to loop thru my list of values and grep against my source file. If it doesn't find the ...
Classified's user avatar
0 votes
3 answers
77 views

How to expand variable inside single or double qoutes

I have 2 systems and they have the same directory strcuture. I want to execute a couple of commands on local system and the same on remote system. I use the wildcards so when I have a new version of ...
DEKKER's user avatar
  • 968
0 votes
1 answer
148 views

How to store command with option on variable of a function in zsh?

I'm trying to learn bash scripting using freeCodeCamp tutorial for beginners on YouTube. I'm stuck at the point where he shows how to create a function. He saved on a variable a command with an option ...
Paul Marcelin Bejan's user avatar
-4 votes
1 answer
61 views

How to protect variable name inside shell arithmetic?

Variables can be protected with curly braces. Mynewvar=1 echo $Mynewvar 1 echo ${Mynewvar} 1 However I can no longer protect them when inside shell arithmetic. Mynewvar=1 echo $((Mynewvar+9)) 10 echo ...
jwav629's user avatar
1 vote
0 answers
99 views

Can you assign a default array to a variable by specifying its values explicitly?

I know that I can use an existing array as the default value for a variable that is potentially uninitialized via default_values=(1 2 'value is a string') array=("${array[@]-${default_values[@]}}&...
da22e's user avatar
  • 11
-4 votes
2 answers
290 views

Is it possible for any shell to interpret a string with decimal point as a number (int, float)?

This idea came to me with recent updates to Visual Studio Code, in which I usually shellcheck my code automatically as well. This is what I see, so you can get the idea: This question is very simple ...
Vlastimil Burián's user avatar
0 votes
1 answer
225 views

How to assign output of Grep in a Variable

I am trying to assign the output of grep in a variable . The below command provide me the correct output in a script str="DWMessStage-20230609-083501.xml__a6e27c1.4a73a37b.13.1889d3efa14.N7f9d....
Shuvra Maitra's user avatar
1 vote
2 answers
203 views

Grep not working - searching a file with a variable

I am trying to use grep to find the lines in a file that start with a specific variable I am defining. I know that you need to use double quotes with this command when searching with a variable, but ...
marissa's user avatar
  • 75
-2 votes
3 answers
788 views

Bash Read First Line of File with Variable Assignment

Unity has a variable assignment in a file called "ProjectSettings/ProjectVersion.txt" but when I try to read it, it tries to run and throws an error. The first line of the file is ...
kirbycope's user avatar
0 votes
1 answer
2k views

Zsh i named variable; "bad math expression: operand expected at `/houdi/loo...'"

this's quite puzzling me, doing Zsh assignment using variable namedly i % for i in '/houdi/lookup' '/komando/' ;{ echo value i = "$i" } zsh: bad math expression: operand expected at `/houdi/...
user avatar
0 votes
0 answers
400 views

Including square brackets in variable values

In the following script #!/bin/bash shopt -s globstar set -e fwd_slsh='/' space=' ' ampersands='&&' bang='!' opnbrc='[' clsbrc=']' caret='^' prefix=${bang}${fwd_slsh}${opnbrc}${caret} suffix=...
Vinod's user avatar
  • 165
2 votes
2 answers
138 views

Parse command with expanded variables

Trying to test something and not quite nailing it either way I've tried, so not sure if I'm being silly or not. Essentially I need to parse a string to ipa-getcert's post-save argument which should ...
dmgeurts's user avatar
0 votes
1 answer
666 views

Add Asterisk to a path Variable?

I need to use *.* at the end of a Path that is stored in a variable. Also just one "*" should be OK as well. finally I want to move files in folders with same name. With a static Path like /...
Donaufisch's user avatar
1 vote
1 answer
245 views

Script to select a PulseAudio sink from an ordered list and use its name as command input

I wish to write a bash script that will create a PulseAudio loopback device between an audio source and a bluetooth device. (Context - skippable) So far I have (the relevant part of) my script as: ...
Marc.2377's user avatar
  • 1,102
0 votes
1 answer
657 views

Shell scripting, reading a list from tt file, use as variable, use the variable to run python scripts in a foor loop

I am trying to run a shell script where it reads the variables from a txt file, uses that variable in a for loop to run python file. The code reads a txt file where at least there are 100 variable ...
BircanA's user avatar
  • 21
0 votes
1 answer
471 views

How to use a path to a directory with a space within variable to cat all files within the directory?

I have a variable with path to folder1 containing a white space: TEST=path/with\ space/to/folder1/ I want to concatenate all files within a folder1 with cat but it gives an error: cat "$TEST&...
trilisser's user avatar
  • 103
2 votes
3 answers
3k views

Variable username in SSH config

I am looking for a way to parse a variable to the SSH config file. Currently I have a script that replaces the values before distribution. I'd rather eliminate this step. I've been over the manual ...
proxx's user avatar
  • 87
0 votes
1 answer
1k views

Can't capture the output of git pull command in a variable

I run git -C /another-directory pull and I get this error: Your configuration specifies to merge with the ref 'refs/heads/main' from the remote, but no such ref was fetched. Now I want to get that ...
Saeed Neamati's user avatar
1 vote
1 answer
68 views

Separate variable scope spawning commands under job control

Is is possible in bash - or other sh-derivative shell - to run in the foreground from command-line a list of commands that have their own variable scope (so any values assigned to variables in that ...
S P Arif Sahari Wibowo's user avatar
2 votes
1 answer
312 views

printf "%.3f" ${variable with newlines} - error with \n

Like HERE I have a file.csv with numbers in quotes: "0.2" "0.3339" "0.111111" To round the number (3 decimals) this solutions works great: printf "%.03f\n" $(...
R 9000's user avatar
  • 167

1
2 3 4 5
25