bash

Bash: variable from commands

To assign the output of a command into a bash variable:

var=$(command)

Bash: passing environment variables

For environment variables to be visible from within a bash script, you must export them.

Given the bash script:

#!/bin/bash
echo $myvar

The environment variable myvar must be set this way:

$ myvar="test"; # This won't work.
$ export myvar="test"; # This works.

Another solution would be to source another script where those variables are defined.
You may source in a sub-shell in order to limit the scope of the variables:

( . ./all_my_variables.sh )

~/.bashrc

The ~/.bashrc file is a bash configuration file.
It is read when starting a new X session provided that bash is set as the default shell.

See also:
http://linux.overshoot.tv/home/user/.xinitrc
http://linux.overshoot.tv/home/user/.xprofile

bash: working with directories

Sample code: check if directory exists, if not, create it. Check if the directory is writable.

if test ! -d "$MYDIR"
then
  mkdir "$MYDIR"
elif test ! -w "$MYDIR"
then
  echo "$MYDIR exists but is not writable."
  exit 3
fi

konsole: Warning: Could not start program '/bin/bash' with arguments '/bin/bash'.

Sometimes, when I start a KDE session where konsole instances are being restored, I find in one of the konsole tabs:

Warning: Could not start program '/bin/bash' with arguments '/bin/bash'.
Warning: No such file or directory
Warning: Could not start program '/bin/bash' with arguments '/bin/bash'.
Warning: No such file or directory

The problem is temporary. Other tabs are ok. I close the affected tab and open a new one without problems.

bash, man command completion

What drives command completion (tab) in bash?

When I want: man rc-update and type man rc and then TAB, I don't get command completion.

Where is my PS1 defined?

When I chroot into my gentoo environment, PS1 is defined but I don't know where.

$ sudo chroot /mnt/gentoo
# echo $PS1
\[\033]0;\u@\h:\w\007\]\[\033[01;31m\]\h\[\033[01;34m\] \W \$\[\033[00m\]
/ # env-update && source /etc/profile
>>> Regenerating /etc/ld.so.cache...
# echo $PS1
\[\033]0;\u@\h:\w\007\]\[\033[01;31m\]\h\[\033[01;34m\] \W \$\[\033[00m\]

I greped but couldn't find where that PS1 was defined...

Find out where alias was set

If I do:
$ alias my_aliased_command
I can find out which command the alias stands for.
e.g.

$ alias ll
alias ll='ls -lh --color=always'
$ alias lla
alias lla='ls -lha'
$ alias la
alias la='ls -A --color=always'

Is there a way to discover we might have set up on our system but forgotten (or simply didn't know about)?

I'd like to be able to do:

$ alias --reverse ls
alias la='ls -A --color=always'
alias lla='ls -lha'
alias ll='ls -lh --color=always'

No manual entry for alias

$ man alias
No manual entry for alias

=> There should be one.

Not all bash builtin commands have their man page

Having man pages for bash builtin commands is convenient.
man echo provides the man page for the bash command 'echo'.

Some/most builtin commands do not have their own man page.
e.g. man read give man 2 read, which is about a c++ library. There is no man page for man 1 read (bash's read).

The official bash documentation is unwieldy.

Currently, my info pages are broken: #5475: The Info node shell builtin commands in Info file /usr/share/info/bash.info does not exist.

Syndicate content