ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
I have a bash script which has long (more than 80 cols) statements like these:
Code:
.....
GNOME_PANEL_ROOT_SETTINGS="$BSP_FILES_LOC_FULL/gnome-panel/gnome-panel-root.entries"
.....
if [[ $BSP_TYPE == "essential" || $BSP_TYPE == "full" || $BSP_TYPE == "livecd" || $QSP_TYPE == "dummy" ]]
.....
echo "The argument 'dummy' is always used with second argument. Please provide an appropriate second argument."
I deliberately picked the above statements which shows three different types of scenario -- Variable declaration, IF statement and a echo statement.
Now, I know I can shorten the length of the above by choosing a different variable or reformatting the sentence but that this is not the point because there can always be a scenario where you have used everything that is ideal but your line still goes beyond 80 cols.
So the question: What is the best way to get the above lines under 80 cols?
Well I do not know of any hard and fast rule that says 80 has to be the limit, but escaping the end of the line should work well.
After that I guess it comes to personal preference. I would probably leave the variable assignment.
Code:
.....
GNOME_PANEL_ROOT_SETTINGS="$BSP_FILES_LOC_FULL/gnome-panel/gnome-panel-root.ent\
ries"
.....
if [[ $BSP_TYPE == "essential" || \
$BSP_TYPE == "full" || \
$BSP_TYPE == "livecd" || \
$QSP_TYPE == "dummy" ]]
.....
echo "The argument 'dummy' is always used with second argument. Please provide \
an appropriate second argument."
i.e. just hit enter key after every variable checking, the script works fine too. What is the difference between the one that I just showed above (and what is it called as?) and the one that you mentioned (escaping the new line with "\").
Nothing is really different that I am aware of ... it was more to keep it looking the same so you knew if you ever see '\' at the end of the line then the information is continued onto the
next line ... again a personal preference.
i.e. just hit enter key after every variable checking, the script works fine too. What is the difference between the one that I just showed above (and what is it called as?) and the one that you mentioned (escaping the new line with "\").
The short answer is, shell interpreters basically ignore white space, which allows us to code our scripts in a human-friendly fashion. So if you have consecutive spaces (all over the place in both examples), it treats them as one space, and if you have a newline character within a shell construct (the if construct, in this case) then it'll ignore them until it finds the end of the construct.
It does this because the computer doesn't care about aligning columns, but it makes things easier for us.
I generally agree with leaving the var assignment as is, although it may make your code more flexible to assign separate vars to dirs and filenames and concatenate as reqd.
I think there is a difference whether you use \ or not (not only personal taste). In the above statement you have the line breaks at a boundary of a statement, but not inside a statement (I think the if construct might not be finished, but the if on its own is finished while the comparison below isn’t finished). So:
Code:
#!/bin/bash
if [[ $BSP_TYPE == \
"essential" ||
$BSP_TYPE == "full" ||
$BSP_TYPE == "livecd" ||
$QSP_TYPE == "dummy" ]]; then
echo yes
fi
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.