Bash shell scripting basic question regarding the syntax
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Bash shell scripting basic question regarding the syntax
Consider the script below:
myname=`basename $0`;
for i in `ls -A`
do
if [ $i = $myname ]
then
echo "Sorry i won't rename myself"
else
newname=`echo $i |tr a-z A-Z`
mv $i $newname
fi
done
1) I am aware that basename $0 signifies my script name here. But how? the syntax explanation please. What does $0 mean?
2) In what cases ";" is used at the end of statement in a script? For example, the first line of script ends with ; , whereas the 8th line doesn't. Also, i found that adding/removing semi-colons at the end of some lines (for example: 1st/6th/8th lines) doesn't really have any significance and script runs fine with or without it.
From man bash, section PARAMETERS, subsection Special Parameters:
0 Expands to the name of the shell or shell script. This is set
at shell initialization. If bash is invoked with a file of com‐
mands, $0 is set to the name of that file. If bash is started
with the -c option, then $0 is set to the first argument after
the string to be executed, if one is present. Otherwise, it is
set to the file name used to invoke bash, as given by argument
zero.
It says 0 there, but $0 is meant because $ identifies a parameter.
You can find such information in man pages easily by using the search key /. Just type /\$0 followed by return. The $ has to be quoted as \$ in this case because the $ has a special meaning when searching, and the backslash is needed to "escape" this special meaning.
In what cases ";" is used at the end of statement in a script?
Usually only when you want to put at least two statements in a single line, for example this would be a case where you can use ;:
if [ $i = $myname ]; then
There's no case in your example script where the ; is required, you can remove it from the first line. Details can again be found in man bash:
Lists
A list is a sequence of one or more pipelines separated by one of the
operators ;, &, &&, or ||, and optionally terminated by one of ;, &, or
<newline>.
[...]
A sequence of one or more newlines may appear in a list instead of a
semicolon to delimit commands.
If a command is terminated by the control operator &, the shell exe‐
cutes the command in the background in a subshell. The shell does not
wait for the command to finish, and the return status is 0. Commands
separated by a ; are executed sequentially; the shell waits for each
command to terminate in turn. The return status is the exit status of
the last command executed.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.