Daily Dev Log

2019 Jun 24

CLI

“foo –help” vs “man foo”

We just can’t remember options of CLI tools. In most cases, --help, like grep --help, is the go-to way to look for help.

For example, if you forget what -H of grep does.

$ grep --help | grep -- -H
  -H, --with-filename       print the file name for each match

man is too formal, wordy and overwhelming, comparing with --help. Usually, we can find what we want in the output of --help, without turning to man.

Another advantage of --help is that it’s always available, and built in the command itself. If you have ever used Git Bash for Windows, you will find that man is not available there. Relying on man only means you have to google the man(ual) in the browsers.

Or to save some typing, add a bash function into ~/.bashrc.

h() { $1 --help; }

Then type h grep to show the help.

Daily Dev Log

2019 Jan 24

CLI

“su - app” vs “su app”

From man su,

   -, -l, --login
      Provide an environment similar to what the user would expect had the user logged in directly.

So with su - app, after switch to the user app, you end up in the user’s HOME directory, and have the user’s ~/.bash_profile (not ~/.bashrc) executed.

Tools like RVM need a “login shell”.

RVM by default adds itself currently to ~/.bash_profile file

So if use su app, RVM will not be ready there for you.

Daily Dev Log

2019 Jan 15

CLI

Convert Line Endings from DOS/Windows Style to Unix/Linux Style

tr -d '\015' <DOS-file >UNIX-file

For what character \015 is, see man 7 ascii. Or, there is a command called ascii, if it’s installed, type ascii '\015'.

More ascii Commands

$ ascii '\r'
ASCII 0/13 is decimal 013, hex 0d, octal 015, bits 00001101: called ^M, CR
Official name: Carriage Return
C escape: '\r'
Other names: 

Search Manuals

-k Search the short descriptions and manual page names for the keyword

$ man -k ascii
ascii (1)            - report character aliases
ascii (7)            - ASCII character set encoded in octal, decimal, and hexadecimal
...

Pitfalls

Do Not Use the Same File to Redirect Input and Output

tr -d '\015' <DOS-file >DOS-file

The above command will delete all content in the file!

From man bash,

[n]>word, if it does exist it is truncated to zero size.

(How did I find the file back? Luckily, the working directory is managed by Dropbox, and I found it back in the Dropbox.)