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)

0
votes
1answer
48 views

sed with line break

I'm trying encrypt a password and replace the word "notset" with this new encrypted password. The problem is that openssl sometimes creates a string with more than one line, like: Salted__▒ہ▒ >▒▒1▒...
1
vote
2answers
1k views

Passing arguments from one script to another

Let's say I am in a Bash script and am sourcing a file. How do I pass variables to another script after I source the file like this. sed -i 's/ = /=/' $file source $file Let's say file contains ...
-2
votes
1answer
38 views

using case statement to read from a variable/file while creating a MENU

read -p 'enter your choice [ 1-5 ]' choice case $choice in 1)abcrequest 2)defresponse 3)afgRequestReply 4)FSfReply 5)ghjrequest I want the case ...
0
votes
2answers
32 views

Add variable number of variables

I need to parse a CSV file for certain fields, and based on matching a pattern I need to add fields together. I've succeeded in setting variables, but need help figuring out how to add them when there ...
0
votes
1answer
15 views

Accessing variable value from inside ansible playbook

I have used the following variable in ansible playbook as shown below. --- - hosts: hosts vars: vers: 18.7 I am trying to access the value of vers using $ under tasks as shown below ...
0
votes
0answers
50 views

Strange counting problem of variable in bash script [duplicate]

I have the following bash script which works fine until I get to number 9 (/dev/sdj) where it does not produce the expected output. hdd1="$(sudo fdisk -l | grep "Disk /dev/sd" | awk '{print$2}' | sed ...
3
votes
0answers
56 views

When coding for Unix/Linux is it convention to use a trailing slash on path names, or to omit it? [duplicate]

When programming for Unix/Linux, is it best practice to use a trailing slash, or to omit it? For example: Trailing slash baseDir="/path/to/dir/" #code# outPath=${baseDir}addition/to/path/ No ...
0
votes
1answer
34 views

echo return an empty variable $

I have this command echo "<?php $x=$_GET['x']; echo $x;?>" > x.php the output file x.php is: <?php =['x']; echo ;?> Has that any relation to the $ variable? and how do I avoid ???, ...
0
votes
1answer
35 views

Translate a single line into a file with one shell command

I'm looking for a convenient command to run from my terminal. At now I'm using $> FILE=./file_for_translate.md && ROW=84 && \ DATA=$(sed -n ''"$ROW"'p' $FILE | trans -no-init -no-...
2
votes
2answers
48 views

Linux Bash: Variable to another variable [duplicate]

i have this: echo $MSG | sed -e $'s/;/\\\n/g' I want to put the result of that sed in a new variable called $MSG2 Something like: $MSG2=echo $MSG|sed -e $'s/;/\\\n/g' How can i do it? Thank you!...
0
votes
2answers
40 views

Cannot escape '?' character in variables (unix)

I have been looking for this question everywhere, but I didn't find it and there doesn't seem to be any kind of answer yet to my current problem, which, frankly, doesn't even make sense. So, what I ...
2
votes
1answer
61 views

How to render variables without quotes in bash script?

The following script: $ cat runme01.sh #!/bin/bash A=myval B=$A/{fix} C=$A/fix set -xT echo $B echo $C prints the following if ran: $ ./runme01.sh + echo 'myval/{fix}' myval/{fix} + echo myval/...
1
vote
1answer
33 views

Inconsistent behavior of bash string replacement

When I print a variable with one substring replacing another as described in this documentation, it behaves correctly: stringZ='abc - 123' echo $stringZ # abc - 123 echo ${stringZ/ - /...
0
votes
1answer
28 views

How to assign a zero-length, non-null string

Based on this, amongst many other things I've read, my understanding is that a='' makes a both null and zero-length. But, then, how does one create a zero-length, non-null string? Or, is there no ...
0
votes
3answers
17 views

Writing a variable into a container's file at a certain line

I need to write a variable into a file which is in a docker container at line 31. I use a bash script my command is: txt=" log-bin server_id=1" docker exec $container sed -i "31i\ $txt /etc/mysql/...
1
vote
2answers
32 views

Bash Perl multiline match into variable

I am trying to extract 2 strings from a multiline text. postmsg="\033[0;37m Email Settings" This line finds some match at the beginning of the string and puts it in a variable inCol. inCol=$(perl -...
2
votes
4answers
156 views

Convert only certain words to lowercase

Say I have a variable var whose value is fOo bar1 baR2 bArab. How do I go about saving into another variable, say, lc_var, a version of var where all but the first word are converted to lowercase? I ...
7
votes
3answers
116 views

Can I duplicate output of a pipe?

Well, the task is simple: a part of my script has to compute both md5 and sha1 hashes. The input is a file - big file - and hashes have to be put into MD and SH variables for later output composition. ...
0
votes
2answers
47 views

Sed escaping “.” and use $variable?

I don't understand this escaping. Is some generator available for it? i need replace normal text (185...) with Variable $NEW_DNS sed -i "185.228.168.168,185.228.169.168|$NEW_DNS /etc/wireguard/wg0....
1
vote
1answer
19 views

Adding a NEW LINE character in between variables

How can I add a new line character (\n) between two Unix variables? I tried the following but it's getting printed in a single line instead of separate lines. h=hello w=world c="$h"$'\n'"$w" echo $c ...
2
votes
1answer
34 views

Why doesn't pattern substitution work from a single variable?

Please see the below session transcript: $ mkdir temp $ cd temp/ $ touch file{1..5} $ ls file1 file2 file3 file4 file5 $ SRC=file; TGT=ram $ for f in file* ; do mv $f ${f/$SRC/$TGT} ; done $ ls ...
2
votes
2answers
35 views

Regex to check arguments of command

I want to use grep to check if a command called cmd that was ran, contains the word name as an argument. (I know about about !* in Bash, I don't want to use it). Let's say I have the command in a ...
2
votes
1answer
82 views

Shell script returning “Running: command not found”. Not sure why

When I am setting a variable to a command, I am getting the answer along with the error "command not found". Below is the command in the script. #!/bin/bash fs=$(ps -C "daemon_name" > /dev/null &...
-1
votes
1answer
31 views

How to Force String Type on a Variable?

I have this script: #!/bin/bash USERS=("user1.com" "user2.co.uk" "user3.com") PASSES=("pass1" "pass2" "pass3") for i in ${USERS[@]}; do echo ${PASSES[i]}; done running this script outputs: ...
0
votes
1answer
34 views

Output of piped command to variable not working

When I try the following there is no problem: $ date '+%y%m' | sed 's/.\(.*\)/\1/' 906 But When I try to store it in a variable it seems nothing is stored: $ dd=`date '+%y%m' | sed 's/.\(.*\)/\1/'` ...
1
vote
2answers
76 views

What is a variable attribute?

I aim to understand the general concept of "variable attributes" hoping it will help me understand what is declare in Bash. What is a variable attribute? Why would someone want to give an attribute ...
6
votes
3answers
893 views

Loop over a string in zsh and Bash

I would like to convert this Bash loop: x="one two three" for i in ${x} do echo ${i} done in such a way to work with both Bash and zsh This solution works: x=( one two three ) for i in ${x[@...
0
votes
0answers
20 views

Assign temp file from cgi and read it in .sh [migrated]

I have .cgi file and .sh file. The cgi is calling the script which is the .sh file. How will assign the tempfile created from .cgi and pass it to the .sh file? Example below: (on my CGI, $tmpfile is ...
0
votes
2answers
112 views

Interpret variables from read in string with shell script [duplicate]

I have a text file which is usually filled with multiple lines which I want to "print" with a while loop. The text inside this file contains variables - my problem is, that these variables are not ...
2
votes
1answer
58 views

Pass variable to cat [duplicate]

I have only one variable in bash script ${PHP_V} and trying to pass in nginx config file like: cat <<'EOF' > /etc/nginx/sites-available/default server { listen 80 default_server; ...
3
votes
2answers
83 views

Get value of the passed argument to script importing variables from another script

I am trying to export variables from one script to the main script and passing one of the imported variables as argument to the main script. Here is the script fruitcolour.sh having variables only : ...
0
votes
1answer
31 views

Variable within path to file in bash script

I've got a file, that contains about 20k strings. Each string in that file should be an argument for a command within bash script. At first my script generates a file with this strings. find /home/...
7
votes
1answer
201 views

Does `declare -a A` create an empty array `A` in Bash?

Does declare -a A create an empty array A in bash, or does it just set an attribute in case A is assigned to later? Consider this code: set -u declare -a A echo ${#A[*]} echo ${A[*]} A=() echo ${#A[*...
0
votes
1answer
33 views

What happens when we run var=3 command [duplicate]

Below I run what I expected to be an invalid command: var=3 date, which in fact isn't. $ var=3 date Sun May 26 17:10:22 UTC 2019 $ echo $? 0 But the variable wasn't assigned the value 3: $ echo $...
1
vote
2answers
27 views

Bash Split Variable into 3 Variables based off Delimiters?

Trying to split a string var with the following pattern: TestString="Artical Title Here (ClassYear) [Author]" I was able to get the title into a Var using cut as such: ATH="$(cut -d'(' -f1 <&...
0
votes
1answer
23 views

add a line to file that contains quotes and variable using bash

I want to add this line to /etc/grub.d/00_header set superusers="ubuntu" where ubuntu is the result of $USER. I've tried this: sudo bash -c 'echo "set superusers='"${USER}"'" >> /etc/grub.d/...
4
votes
3answers
1k views

Unable to assign output of nested commands to variable in bash

I was trying to assign below command(which choose the random line from file) to a variable, but not working. givinv@87-109:~$ head -$((${RANDOM} % `wc -l < file` + 1)) file | tail -1 cower givinv@...
4
votes
3answers
259 views

Iterate lines of string variable in bash

I have a script where I want to list USB devices using the command lsblk. The command: $ lsblk -o NAME,TRAN,VENDOR,MODEL | grep usb which results in sdb usb Kingston DataTraveler 2.0 sdc ...
7
votes
3answers
4k views

List variables with prefix where the prefix is stored in another variable

I'm trying to list all the variables with a certain prefix, but that prefix is dynamically determined. For example: prefix=apple_ apple_one=a1 apple_two=a2 If I simply do: echo ${!apple_@} I can ...
0
votes
3answers
673 views

Using `printf` to print variable containing `%` percent sign results in “bash: printf: `p': invalid format character”

I want to use printf to print a variable. It might be possible that this variable contains a % percent sign. Minimal example: $ TEST="contains % percent" $ echo "${TEST}" contains % percent $ printf ...
0
votes
2answers
142 views

new lines and bash variable

I have a text file (or pipe output, doesn't matter here) memcached.uptime 1061374 memcached.curr_connections 480 memcached.cmd_get 478962548 memcached.cmd_set 17641364 memcached.cmd_flush 0 If I use ...
1
vote
0answers
19 views

How to clear DISPLAY variable in GNU screen

Basically, i need to unset DISPLAY variable in screen if I run multiple commands like: screen -dmS name bash -c 'kodi; unset DISPLAY; emulationstation' I'm writing a little something that will ...
23
votes
5answers
2k views

Why use double quotes in a [[ ]] test?

Let's say we have 2 integers in a bash script: value1=5 value2=3 Then why do we need to use double quotes in case of a test ? For example: if [[ "$value1" -eq "$value2" ]] Why not just use the ...
1
vote
2answers
95 views

Variable initialization with internal variable

How would I get this to work in ksh? variable1="$(strings /.sh_history | grep '^\user' | tail -1 | cut -d' ' -f2)" variable2="$(command ${variable1})" Basically, I would like to use variable1 as a ...
1
vote
2answers
31 views

Linux Shell - Certain Variables and Methods NOT appending to text file

I'm having an issue getting the variables $myname, $filename, and a method that counts all the records in another file, wc -l < hs_alt_HuRef_chr10.fa >> "$CuestaP.txt, to append to the text ...
0
votes
1answer
29 views

Evaluate command based on variable at bash [closed]

I am writing a small script that would choose random file for me from the directory and execute command with this chosen filename as a parameter. It is about choosing random .ovpn file and run sudo ...
0
votes
1answer
22 views

Why can't echo and printf output variables that have only whitespace characters? [duplicate]

var1=$(printf "\n\x0A\n") var2=$(printf "\na\nb\nc") Using Android Terminal, the output of echo -e "$var1" (same for echo and printf) iss nothing, not even the 3 new lines. But echo -e "$var2" or ...
0
votes
1answer
56 views

very long string variable?

Is there a longer type variable for building lengthy sql statements? how add new line too? I want to store the statement in a very long variable text so I can run it from sql prompt. I think it will ...
0
votes
1answer
44 views

How do i define a directory in variable so as to back up file to?

I am trying to understand my logic in this very basic script. I am trying to accomplish something extremely simple. I am trying to copy /var/log/messages into a directory (under /) called backup as ...
0
votes
1answer
29 views

awk save value as variable

I use the command varioutput=$(awk '{print $j}' OFS=, data/damper.test_temp1.csv) because I want to extract the j th value of a line of many values seperated by , . But when I want to use $...