[SOLVED] bash: So simple yet so impossible to find (elsewhere).
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's 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: So simple yet so impossible to find (elsewhere).
I seem to be forgetting little things about bash scripting. Such as: what is the # to ignore a command in a script called (cancel or nullify are the only things that comes to mind.)
As in this example:
Code:
#if [ "$(ls *.bak 2>/dev/null)"]; then
Carver
Last edited by L_Carver; 09-13-2018 at 01:02 PM.
Reason: Another parenthetical 'option' added.
That is called "commenting" - it is not so much about cancelling a command, although that is a frequent and useful way to use it! It provides a way for you to add comments to your code - everything after the # is ignored by the language interpreter.
It is easy to find in man bash
Code:
COMMENTS
In a non-interactive shell, or an interactive shell in which the interactive_comments option to the
shopt builtin is enabled (see SHELL BUILTIN COMMANDS below), a word beginning with # causes that word
and all remaining characters on that line to be ignored. An interactive shell without the interac-
tive_comments option enabled does not allow comments. The interactive_comments option is on by default
in interactive shells.
Last edited by astrogeek; 09-13-2018 at 01:08 PM.
Reason: Added man bash excerpt
just remember, the comment sign is only valid for one line, nothing more. So if you comment an if (like in your own example) you need to comment all the lines below to the closing fi. Otherwise you will (may) get syntax errors.
(Almost) all the programming languages has commenting features, it is not really bash specific thing.
For example in shells you can use #, in C++ there are 2 kind of comments available (// for single line, /* ... */ for multi-line comments), also you can have comments in sql (using --), perl (#), python (#), java (like C++) and so on
just remember, the comment sign is only valid for one line, nothing more. So if you comment an if (like in your own example) you need to comment all the lines below to the closing fi. Otherwise you will (may) get syntax errors.
(Almost) all the programming languages has commenting features, it is not really bash specific thing.
For example in shells you can use #, in C++ there are 2 kind of comments available (// for single line, /* ... */ for multi-line comments), also you can have comments in sql (using --), perl (#), python (#), java (like C++) and so on
Yes, I remembered that much. It was just what that was called that I'd forgotten.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.