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,232 questions
0
votes
0
answers
40
views
LF file manager : how to go back to $OLDPWD?
What do I want :
On the file manager lf, we can bind some keyboard shortcuts to do either a lf command with :, or a shell command with $, !, or other symbols.
According to the documentation, there is ...
0
votes
1
answer
63
views
bash conditional variable assignment cli not what I expected [duplicate]
Can anyone explain what's going on with DR here?
user@host:~# DRYRUN=true
user@host:~# echo "$DRYRUN"
true
user@host:~# $DRYRUN && export DR="-n"
user@host:~# echo $DR
user@...
0
votes
0
answers
38
views
bash subshell execution behaves unexpectedly [duplicate]
I have a script which is supposed to fetch 2 URLs sequentially:
#!/bin/bash
wget_command='wget --restrict-file-names=unix https://www.example.com/{path1,path2}/'
$($wget_command)
echo $wget_command ...
0
votes
2
answers
189
views
How to apply an ls to a file name with spaces in a variable
This code
my_file="/tmp/file_without_spaces"
if [ -f "${my_file}" ]; then
my_ls_aaaammgg_hhss="$(ls ${my_file} -l --time-style='+%Y%m%d_%H%M%S' | cut -d' ' -f6)"
mv &...
-1
votes
2
answers
106
views
set output into variable from grep from input variable
whats wrong with this bash script:
acme2=$(dig txt @$1 _acme-challenge.$1.de)
acme3=$(echo $acme2 | grep "^_acme") ...
-3
votes
1
answer
88
views
Need working examples of using tilde expansion immediately following the : (colon) sign in variable assignment
In the Bash manual, it is written about the tilde expansion:
Each variable assignment is checked for unquoted tilde-prefixes
immediately following a ‘:’ or the first ‘=’.
Read the Bash manual about ...
1
vote
3
answers
146
views
Bash/macOS: Getting a variable by its name, inside of a function [duplicate]
In Bash for macOS, I need to be able to pass a string to a function and return the value of a variable named after that string, as well as the string itself.
Here's what I have so far, which doesn't ...
1
vote
1
answer
90
views
Zsh’s autocompletion options from variable
General overview
I have to set a values auto-completion in zsh for a command (in the following minimal example, I will show it with testcmd).
So my current code work quiet well with hardcoded values:
...
1
vote
1
answer
60
views
How To Use Quotes In Variable In Bash [duplicate]
I need to assign a command to a variable and execute it with variable in linux bash. The command executes fine from command line but not from the variable because of multiple quotes. I hope backslash ...
5
votes
4
answers
623
views
Getting multiple variables from the output of docker exec command in a bash script?
I would like to execute a script on a host machine (script_on_host.sh), which then reaches inside a docker container in order to grab some data using a second script (script_in_container.sh). The data ...
3
votes
1
answer
210
views
Write the contents of a .conf file into a variable located in a separate .conf file
I am configuring my hyprland.conf file and I am attempting to do it in a clean and modular fashion. I am trying for a modular fashion so that I can share my dotfiles and someone else can change which ...
-1
votes
1
answer
159
views
Bash local nameref declaration does not work
I am trying to set a local variable in a function with nameref.
The script code is the following:
#!/usr/bin/bash
msg=hello
myparam=''
superfunc () {
productfile=$1
local -n refmyparam=$2
}
...
1
vote
1
answer
93
views
How can you echo a variable name made of variable names I.e $$var1$var2 [duplicate]
In this test I'm expecting it to print "var1 is 999".
user@penguin:~$ for num in {1..3}; do export var$num=9999 ; echo var$num is $var$num ; done
var1 is 1
var2 is 2
var3 is 3
user@penguin:~...
2
votes
2
answers
523
views
Zsh Expanding Variables into Arrays or Lists
I looked through some of the posted threads and none of them cover my query.
I have a simple line of code below which prints all the ASCII characters:
echo {' '..'~'}
I want to be able to use a ...
0
votes
3
answers
413
views
Convert json data to comma separated string
I have a json object that has an unknown number of variables, like this:
{
"var1": 123,
"var2": 456,
"var3": 789
}
How can I automatically convert it to form ...
5
votes
1
answer
366
views
How to make MAKEFLAGS=--warn-undefined-variables apply to current Makefile?
Yes I read
Communicating Options to a Sub-'make'
but I want to set an option for this make, not a sub-make,
and set it within the Makefile, not via the command line.
$ cat Makefile
MAKEFLAGS = --warn-...
0
votes
1
answer
57
views
Does Bash have an option to diagnose "expanded to empty value" variables? [duplicate]
Does Bash have an option to diagnose (and optionally abort execution) "expanded to empty value" variables?
Example (hypothetical):
$ bash -c 'echo $x' --xxx
bash: line 1: variable 'x' ...
2
votes
1
answer
482
views
What's the difference between '$var' and 'var' in an arithmetic expansion?
Bash accepts both these syntax:
FOO=$(($BAR + 42))
and
FOO=$((BAR + 42))
Which one is correct / most portable / less prone to errors? Or are both equally valid?
7
votes
1
answer
282
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 ...
0
votes
1
answer
217
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 ...
-1
votes
1
answer
56
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 --...
0
votes
2
answers
110
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 ...
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:
#...
0
votes
0
answers
256
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 ...
7
votes
1
answer
2k
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?
0
votes
2
answers
221
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 ...
5
votes
2
answers
526
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 ...
0
votes
1
answer
87
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 ...
0
votes
1
answer
82
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 ...
5
votes
1
answer
331
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 ...
0
votes
1
answer
134
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 ...
1
vote
1
answer
153
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 ...
5
votes
2
answers
1k
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 {
...
1
vote
0
answers
46
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\]:\[\...
-1
votes
1
answer
281
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 ...
0
votes
1
answer
481
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 ...
1
vote
0
answers
180
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-...
1
vote
2
answers
251
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 ...
1
vote
1
answer
363
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 ...
0
votes
1
answer
186
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 ...
1
vote
1
answer
221
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-...
0
votes
1
answer
668
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=&...
0
votes
1
answer
76
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: ...
1
vote
3
answers
97
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 ...
0
votes
2
answers
610
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 ...
1
vote
0
answers
419
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 ...
4
votes
2
answers
2k
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 ...
0
votes
2
answers
441
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 ...
0
votes
3
answers
179
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 ...
0
votes
1
answer
325
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
...