All Questions
643 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
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 ...
-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:~...
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 ...
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?
0
votes
1
answer
162
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
45
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
76
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:
#...