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,230 questions
0
votes
2
answers
72
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
67
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
76
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
84
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
53
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
46
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
520
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
143
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
78
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
72
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
269
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
213
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
230
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
40
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
450
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?