variable

printf – store formatted string output in a variable

printf – store formatted string output in a variable Instead of the following printf ‘%st%st%st%sn’ ‘index’ ‘podcast’ ‘website’ ‘YouTube’ I want to store the printf output in a Results variable, how can I do this? Asked By: Porcupine || Source Bash (since 3.1), zsh (since 5.3) and ksh93 (since v- and u+m 2021-11-28) have an …

Total answers: 1

How To Have GNU Parallel Pick up Variables Defined Outside of It?

How To Have GNU Parallel Pick up Variables Defined Outside of It? My actual case is more complicated, but this is the problem I’m having (the question in the title. Simplified Example echoSimultaneously.bash: echoSimultaneously() { parallel -j 0 echo ::: $string } run() { local IFS string # split on new lines not on white …

Total answers: 1

Bash brace expansion doesn't work in an export statement

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 expansion didn’t work 🙁 How come that if I echo file_{a,b,c} directly, the brace expansion prints the name of all three files, …

Total answers: 1

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

Why does -n with unquoted variable containing empty string return true? 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 $? 0 Here we see that -n with unquoted variable …

Total answers: 1

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

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 { echo "COUNTER: $COUNTER ($1)" } function reset_counter { COUNTER=0 } function increment_if_yes { answer=$1 if …

Total answers: 2

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

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 the POSIX man page, as for example if I can single quote or have to …

Total answers: 1

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

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 …

Total answers: 1

Change hostname with a variable with for loop

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 variable, change it automatically. When I run it, it tells me "Invalid number of …

Total answers: 1

some_variable vs some-variable in zsh

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-screen=1 mpv –fs-screen=${fs-screen} someFile.mp4 results in zsh: command not found: fs-screen=1 I went digging through the zsh documentation and found what I …

Total answers: 1

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

Bash script variable syntax: with some commands it works, with others it does not 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: cannot access ‘~/.ssh/config’: No such file or directory -rw——- 1 pm domain^users 1229 Sep 19 10:52 /home/pm/.ssh/config …

Total answers: 1

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

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 GNU sed statement: sed …

Total answers: 3

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

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 be any other string not just Test e.g. CDB79 etc.) part only in …

Total answers: 2

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

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 films to the user and take input from them …

Total answers: 2

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

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 …

Total answers: 2

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

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 #!/bin/bash showuptime(){ up=$(uptime -p | cut …

Total answers: 1

Grep not working – searching a file with a variable

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 it is just printing an …

Total answers: 2

Bash Read First Line of File with Variable Assignment

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 m_EditorVersion: 2021.3.6f1 % $(head -n 1 ProjectSettings/ProjectVersion.txt) > zsh: command not found: m_EditorVersion: I …

Total answers: 3

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

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) The audio source is created with: load-module module-jackdbus-detect channels=2 sink_name=jack_bt_eq_out sink_client_name="PulseAudio – Bluetooth …

Total answers: 1