Skip to main content

All Questions

2 votes
1 answer
841 views

issues with $ symbol while reading from positional parameters in unix shellscript

I have written the following script: #!/bin/bash TEST=$1 TEST_1=$2 if [[ "$TEST" == e ]];then echo $TEST_1 else echo "Input is not e" fi Now if I will run: sh test.sh e NS3#$xX$...
John's user avatar
  • 73
6 votes
3 answers
11k views

How to prevent parameter expansion around a variable I want to be resolved?

Edit: Important note before reading further: since Kusalananda's anwser, my question is now quite useless, since he pointed out a bug in another script I used. Now this bug is fixed, the below errors ...
el-teedee's user avatar
  • 362
1 vote
2 answers
2k views

Shell Script: how to expand a variable into quotes

Can anyone tell me how can I make the follow command to work properly? SERVER=192.168.1.1 ping $SERVER (It Works) ping '$SERVER' (It doesn't work) I want this to compose a more complex command that ...
Kellyson's user avatar
  • 135
21 votes
2 answers
13k views

How Can I Expand A Tilde ~ As Part Of A Variable?

When I open up a bash prompt and type: $ set -o xtrace $ x='~/someDirectory' + x='~/someDirectory' $ echo $x + echo '~/someDirectory' ~/someDirectory I was hoping that the 5th line above would have ...
Andrew's user avatar
  • 312
0 votes
1 answer
641 views

Do I need to double quote a variable?

I am confused on when we double quote shell variables. Specifically I am using the following sed replace command: sed -i.tmp "/$MY_VAR/d" /foo/bar/file.txt But I am not quoting $MY_VAR. Is this ...
Jim's user avatar
  • 10.5k
1 vote
1 answer
4k views

Basic syntax explanation for variables in bashrc

I want to correctly export a variable. In order to do so I have to understand several variations in syntax. I've seen the following mechanisms of exporting a variable. In this case I am using ....
P.Brian.Mackey's user avatar
0 votes
1 answer
2k views

Print a variable in single quote on bash | Weird Variables

On bash script, I need to pass date as ansible extra variable but getting something single quotes related issues; ... $DT="03-04-17" ansible-playbook copy2s3.yml --extra-vars 'cdate={{ "$DT" }}' and ...
Nullpointer's user avatar
2 votes
2 answers
144 views

shell command 'var="[a-p]"; echo $var' has as output "h", any explanation? [duplicate]

bash version: $ bash -version GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu) Copyright (C) 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu....
Amine Chikhaoui's user avatar
3 votes
3 answers
218 views

What situations exist where Bash variables should not be double quoted? [duplicate]

I'm aware of several situations where it's unnecessary to use double quotes when expanding a variable in Bash, such as inside [[...]], or when used on the right hand side of a variable assignment. ...
Arronical's user avatar
  • 280
20 votes
6 answers
1k views

Should variables be quoted when executed?

The general rule in shell scripting is that variables should always be quoted unless there is a compelling reason not to. For more details than you probably want to know, have a look at this great Q&...
terdon's user avatar
  • 250k
1 vote
1 answer
6k views

Script with variable in path

I'm not entirely sure where to ask or how to formulate this question with correct terminology. I have script here for managing a game (minecraft) server for the user (bukkit). I've cut out some ...
OskyEdz's user avatar
  • 13
2 votes
1 answer
2k views

Inserting variables in a command within a shell script

I'm attempting to write an automated script which will update the IP address for my Raspberry Pi within my /etc/hosts file. I'm able to execute this line just fine, IP=`sudo nmap -sP 10.61.4.0/24 | ...
user138741's user avatar
1 vote
2 answers
2k views

A variable in Bash that contains quotation marks and spaces

I'm trying the following in a Bash script: MV_PARAMS='"foo 1" "foo 2"' mv $MV_PARAMS What I want to actually execute is: mv "foo 1" "foo 2" But it doesn't seem to work. trying this: mv "$MV_PARAMS" ...
Yaron Cohen-Tal's user avatar
4 votes
1 answer
155 views

"$@" expansion for user defined variables

I'm trying to get a (bourne shell) variable to expand like "$@" so it produces multiple words with some having preserved spaces. I've tried defining the variable in many different ways but still can't ...
user126144's user avatar
2 votes
3 answers
4k views

Variable expansion inside parentheses and quotes

In the script below, I can't seem to make $var1 expand in the second statement. I've tried $var1, ${var1}, echo $var1 and '$var1'. It is inside a few sets of quotes and parentheses which I guess is ...
Sco's user avatar
  • 123

15 30 50 per page