8

Every time I need to do a little bash scripting involving if statements, checking for empty variables and non-existent files et.c. I find myself hitting google for some simple bash manual or guide.

Often these are way too long and complicated to quickly answer the question I have at hand.

So, I was thinking that there must be some command or man page that covers the basic bash syntax (for things like if-statements, checks for empty variables, non-existent files etc) in a short and accessible manner. I'm sure I just havent found out the command (man bash doesn't seem to be it).

Where can I find that?

Braiam
  • 35,991

3 Answers3

13
  • man bash is the canonical Bash reference.
  • help help gives you help on the help Bash builtin, and how it gives you short help messages about other Bash built-ins, such as help for.
  • Greg's Wiki is the best place to find to-the-point, terse, and sometimes even entertaining reference material for pretty much every aspect of Bash.
  • If you want a quick way to look up syntax, you could version control .bash_history. There are just too many commands, different parameter syntaxes, sed and awk line noise and other stuff to ever remember all of it, so it's nice to have a "well groomed" personal reference with tags to look up in.
l0b0
  • 51,350
  • 1
    Thanks! As mentioned in @polym's answer I had obviously used the wrong search terms in man bash ("if statements", "[ -n" etc) to no avail, and so thought the info was not there. Searching for "conditional expressions" was the right search term there. – Samuel Lampa Jun 23 '14 at 14:23
  • 2
    I have equal amounts of love and hate for this moment... 10 years...didn't know help was there. So much needless man bash reading not required! – Matt Jun 23 '14 at 16:44
13

Since there are plenty of such things online, here's a suggestion for you; I use this for all kinds of things.

Create a directory $HOME/notes. Add a subfolder, bash. If you don't have a $HOME/bin, create one, add it to your path using whatever method you prefer (for example: in ~/.bashrc). Then put a simple script in there, cmdref.sh:

#!/bin/bash

if [ -z "$CMREF_DIR" ]; then
        CMREF_DIR="$HOME/notes"
fi

cat "$CMREF_DIR/${1}/${2}.txt"

Symlink it for convenience, ln -s ~/bin/cmdref.sh ~/bin/cmdref; I like to do this so you can edit the cmdref.sh source and an editor will still recognize the filetype by the suffix.

Copy paste whatever into various files in ~/notes/bash -- e.g., you could have a file arrays.txt, loops.txt, etc. As long as this is just for your own personal use, you're not violating copyright.

You can now use that from the command line, e.g.: cmdref bash arrays and that cheat sheet will be printed to the console. Note although the files are .txt (again, using appropriate suffixes is useful for editors, file browsers, etc.) you should not use cmdref bash arrays.txt since it's appended in cmdref.sh (the only thing the script does is convert paths and add the suffix).

goldilocks
  • 87,661
  • 30
  • 204
  • 262
  • Gorgeous! Great idea, and taught me a few things I hadn't considered about how flexible and customizable Linux is. – mightypile Jun 24 '14 at 02:18
  • @goldilocks I didn't understand the reason you mentioned for symlinking the script file, particularly "I like to do this so you can edit the cmdref.sh source and an editor will still recognize the filetype by the suffix." Can you please explain? Does that mean I can do vi cmdref and vim will open the "cmdref.sh" and then I can edit it or am i missing something obvious here? – Geek Aug 04 '14 at 18:50
  • @Geek AFAIK all editors primarily use the file suffix to determine the file type WRT syntax highlighting (doing it some other way would be sketchy at best, but might be a fallback for some tools if they do not recognize the suffix). So if you vi ~/bin/cmdref, vim doesn't really know what kind of file this is, and will use a default for highlighting (I think it is .txt). But if you vi ~/bin/cmref.sh, vim knows the file is a shell script and the highlighting will make sense. But WRT a command, you want to type cmdref, not cmdref.sh. That's why I use the symlink. – goldilocks Aug 04 '14 at 19:20
  • As a sidebar to that: vi is itself usually a symlink to vim, which is why you get syntax highlighting at all (the original "vi" did not have such a feature, I think). And: vim actually is smart enough to recognize that script as a shell script if the file has no suffix, but the principle stands ;) On rare occasions maybe I need/want to use something less smart. – goldilocks Aug 04 '14 at 19:21
  • ...if you want to see the difference, load a .sh file in vim and set filetype=txt -- eek! – goldilocks Aug 04 '14 at 19:26
3

man bash covers every aspect of bash syntax, e.g. for if statements, string is zero and non-existent files, just search ( hit the / key and enter the following ) for CONDITIONAL EXPRESSIONS.

polym
  • 10,852
  • 1
    Ah, indeed. My brain must be turned off, or be in lack of as I didn't come up with that search term (I tried searching for "if statements", "'[ -n" etc. to no avail). Thanks! – Samuel Lampa Jun 23 '14 at 14:19
  • 1
    @SamuelLampa if you use the bash man page enough (it doesn't take that much) you'll get a feel for its structure, so you can tell what's in there and about where it is before you actually look it up. – David Z Jun 24 '14 at 01:54