LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 09-13-2018, 01:00 PM   #1
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 243

Rep: Reputation: Disabled
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.
 
Old 09-13-2018, 01:04 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
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
 
1 members found this post helpful.
Old 09-13-2018, 01:26 PM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,899

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
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
 
3 members found this post helpful.
Old 09-22-2018, 07:03 AM   #4
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 243

Original Poster
Rep: Reputation: Disabled
Thanks again for the tipo

Quote:
Originally Posted by pan64 View Post
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.

Carver
 
Old 09-22-2018, 07:32 AM   #5
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by L_Carver View Post
Yes, I remembered that much. It was just what that was called that I'd forgotten.

Carver
That symbol is actually multi-named.

Knock yourself out: https://en.wikipedia.org/wiki/Number_sign

We in the UK at least normally call it a "hash".
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash Issue with Find Script: find paths must precede expression: *.txt JockVSJock Programming 4 01-06-2014 09:03 PM
How to get some bash scripts into a simple bash script with some echo and if statement. y0_gesh Programming 3 03-01-2012 09:46 AM
Gtk-WARNING **: Impossible to find the «smooth» theme engine in module_path maiden2 Ubuntu 2 04-30-2009 06:32 AM
simple bash q adam_boz Linux From Scratch 1 06-08-2003 11:22 AM
It is impossible to find my http://localhost jjay02 Linux - Newbie 3 03-01-2003 04:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration