Bash scripting, question about a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash scripting, question about a variable
# 1  
Old 04-19-2010
Question Bash scripting, question about a variable

Hey,

So I've run into a problem, due to my limited knowledge of Bash scripting.

Basically I've got a long script and I want to understand it before I even try and edit it. As long as I don't understand the script, I will not bother editing it.

Anyway, the following variable confuses me (I don't know what it does, exactly..):

Code:
DIR=$1/*

So my question is basically is someone could tell me (explain to me) what it means.

I know the variable DIR is being created and that the value of the first parameter is being used, but further than that I can't really place it.

I hope someone can help me out.Smilie

Thanks!!
# 2  
Old 04-19-2010
If you are unsure about the content why don't you just display it?

Code:
DIR=$1/*
echo "$DIR"

This line appends a literal "/*" to whatever is given as the first positional argument. Suppose this is inside a script "foo.sh" and you call this script with "foo.sh bar" the variable DIR would contain "bar/*", if you call it with "foo.sh /path/to/somewhere" it would contain "/path/to/somewhere/*".

The statement is poorly phrased, btw., because it is asking for troubles: the asterisk ("*") has a special meaning to the shell and to protect such special characters from being interpreted one should cover them in quotes:

Code:
DIR="$1/*"

Furthermore, when using variable-names without spaces surrounding them it is a wise precaution to explicitly mark what belongs to the variable identifier and what doesn't:

Code:
DIR="${1}/*"

In your case this makes no difference, but consider:

Code:
a="blabla"
b="$ab"     # appending "b" will not work, because a variable named "ab" is expected
echo "$b"

b="${a}b"  # this will work
echo "$b"

I hope this helps.

bakunin
# 3  
Old 04-19-2010
Quote:
Originally Posted by bakunin
The statement is poorly phrased, btw., because it is asking for troubles: the asterisk ("*") has a special meaning to the shell and to protect such special characters from being interpreted one should cover them in quotes:

Code:
DIR="$1/*"


It is not necessary to quote an assignment unless it contains literal whitespace.
Quote:
Furthermore, when using variable-names without spaces surrounding them it is a wise precaution to explicitly mark what belongs to the variable identifier and what doesn't:

Code:
DIR="${1}/*"


There's no point to using braces when the name is followed by a character that cannot be contained in a variable name.
Quote:
In your case this makes no difference, but consider:

Code:
a="blabla"
b="$ab"     # appending "b" will not work, because a variable named "ab" is expected
echo "$b"

b="${a}b"  # this will work
echo "$b"

I hope this helps.

bakunin

Last edited by cfajohnson; 04-20-2010 at 02:54 AM..
# 4  
Old 04-20-2010
Hey,

Thank you bakunin for your fast reply. I finally get it now Smilie.

Thanks!!
# 5  
Old 04-21-2010
Quote:
Originally Posted by cfajohnson
It is not necessary to quote an assignment unless it contains literal whitespace.
True. You can guarantee that "$1" will never contain a whitespace, then? Quoting variables out of habit, even in cases where it is not necessarily required, is just staying out of troubles, doing otherwise is asking for them.

Quote:
Originally Posted by cfajohnson
There's no point to using braces when the name is followed by a character that cannot be contained in a variable name.
Of course there is a point and i named it: precaution. That in this specific case it would not be necessary i did say explicitly said in the very next sentence, explaining on a counterexample what i meant.

We might disagree philosophically about what constitutes robust programming practices: i believe that many aspects of any often-employed activity (like, for instance, programming for a programmer) is driven by habits as much as conscient effort. My experience is that when i act habitually in a safe way i am generally better off then when i habitually employ a potential hazard - i will probably forget to take the necessary precaution in the one case where it will matter.

To come back from the general to the problem at hand, variable expansion: all i "risk" are two keystrokes and 2 bytes on a disk - compared to the avoided potential problems in cases like the one i cited this is not too much, i suppose.

bakunin
# 6  
Old 04-21-2010
Quote:
Originally Posted by bakunin
True. You can guarantee that "$1" will never contain a whitespace, then?

It doesn't matter whether $1 contains white space or not. That is not literal whitespace. $1 could be $' \t\n' or all spaces and it wouldn't matter.

Literal whitespace means an actual space on the command line, not contained in a variable.

(I should also include other characters special to the shell, such as a semi-colon.)
Quote:
Quoting variables out of habit, even in cases where it is not necessarily required, is just staying out of troubles, doing otherwise is asking for them.

Doing it out of habit, instead of out of knowledge, is not good practice.

Doing it where it is never necessary is not good practice.
Quote:
Of course there is a point and i named it: precaution. That in this specific case it would not be necessary i did say explicitly said in the very next sentence, explaining on a counterexample what i meant.

We might disagree philosophically about what constitutes robust programming practices: i believe that many aspects of any often-employed activity (like, for instance, programming for a programmer) is driven by habits as much as conscient effort. My experience is that when i act habitually in a safe way i am generally better off then when i habitually employ a potential hazard - i will probably forget to take the necessary precaution in the one case where it will matter.

To come back from the general to the problem at hand, variable expansion: all i "risk" are two keystrokes and 2 bytes on a disk - compared to the avoided potential problems in cases like the one i cited this is not too much, i suppose.

There is no potential problem in not quoting the assignment of one variable (or a concatenation of variables or strings not containing special characters) to a variable.

OK, I'll agree that quoting the assignment doesn't hurt, but it is not necessary, and certainly not more robust.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Total Noob BASH scripting question

Hello All, I have a file of ip addresses called activeips.txt What I'm trying to do is run a simple bash script that has a loop in it. The loop is a cat of the IP addresses in the file. The goal is to run 2 nmap commands to give me outputs where each address in the list has an OS... (11 Replies)
Discussion started by: Dirk_Pitt
11 Replies

2. Shell Programming and Scripting

Beginner Bash Scripting Question

Hello, I am new to Linux and studying to become a Unix System Admin. I am taking a course in which I was practicing creating a bash script to ping a particular IP address. The script can be found below: #/bin/bash echo "Enter the IP address" read ip if then ping -c 1 $ip if ;... (3 Replies)
Discussion started by: shah9250
3 Replies

3. Shell Programming and Scripting

Bash question: working with an array of previously set variable strings

while i've used arrays to work with variables, i've never used them to loop through a set of strings and wanted to ask the community for some feedback or assistance. let me be specific. here's my code: # URL port Variables port2195=`nc -z $url2195 2195` port2196=`nc -z $url2196 2196`... (5 Replies)
Discussion started by: hungryd
5 Replies

4. Shell Programming and Scripting

Bash variable assignment question

Suppose I have a file named Stuff in the same directory as my script. Does the following assign the file Stuff to a variable? Var="Stuff" Why doesn't this just assign the string Stuff? Or rather how would I assign the string Stuff to a variable in this situation? Also, what exactly is... (3 Replies)
Discussion started by: Riker1204
3 Replies

5. Shell Programming and Scripting

{bash scripting} Multiline initialisation of string variable

Hello, The task is quite simple. I need to initialise а string variable and assign to it a very long value. For the reason of readability I want to devide this long value into equal parts and place each part at a separate line. For instance, I have - ... (1 Reply)
Discussion started by: Pretoria
1 Replies

6. Shell Programming and Scripting

Bash scripting question

have a script that calls child scripts depending on conditions. All of the child scripts source in a common file that contains shared functions. At the moment each script has to source this file itself, is there a way for the master script to automagically source the file for them? For... (3 Replies)
Discussion started by: redman
3 Replies

7. Shell Programming and Scripting

BASH: How do I grep on a variable? (or simmilar question that makes sense)

Hi, I've been running code which very frequently calls books.csv. e.g: grep -i horror books.csv > tempExcept, I'm trying to move away from using temporary files or frequently calling books.csv to improve efficiency. So I tried something like bookfile=$(cat books.csv) grep -i horror... (4 Replies)
Discussion started by: Quan
4 Replies

8. Shell Programming and Scripting

bash and ksh: variable lost in loop in bash?

Hi, I use AIX (ksh) and Linux (bash) servers. I'm trying to do scripts to will run in both ksh and bash, and most of the time it works. But this time I don't get it in bash (I'm more familar in ksh). The goal of my script if to read a "config file" (like "ini" file), and make various report.... (2 Replies)
Discussion started by: estienne
2 Replies

9. Shell Programming and Scripting

passing variable from bash to perl from bash script

Hi All, I need to pass a variable to perl script from bash script, where in perl i am using if condition. Here is the cmd what i am using in perl FROM_DATE="06/05/2008" TO_DATE="07/05/2008" "perl -ne ' print if ( $_ >="$FROM_DATE" && $_ <= "$TO_DATE" ) ' filename" filename has... (10 Replies)
Discussion started by: arsidh
10 Replies

10. Shell Programming and Scripting

Bash scripting question re: newlines

I'm writing a bash script to search the contents of a postfix log. To keep the script's output readable (since multiple lines from the log file need to be echo'ed) I am setting the IFS variable to an empty string so that the line breaks in my grep results are preserved. I am storing the results... (4 Replies)
Discussion started by: retrovertigo
4 Replies
Login or Register to Ask a Question