Difference between revisions of "Bash"

From HalfgeekKB
Jump to navigation Jump to search
Line 24: Line 24:
 
   echo item: $i
 
   echo item: $i
 
  done
 
  done
 +
 +
To get a list of files to which you know the extension and want to omit it in the loop, try
 +
 +
for i in $( ls *.png | sed "s/\\.png$//" ); do
 +
...
 +
 +
for .png files.  Incidentally, you can strip an extension off of a filename in a variable by doing
 +
 +
$( echo -n $FILENAME | sed "s/\\.png$//" )
  
 
=Startup files=
 
=Startup files=

Revision as of 12:56, 4 August 2005

Attention Feminazis

If you are an overzealous feminist and seek documentation for the Bourne Again Shell, type

man bash

at the prompt. (rimshot)

Programming

See also the BASH Programming - Introduction HOWTO.

For

Bash for loops are a little tricky. The general form is

for i in $( ls ); do
  echo item: $i
done

That's how to run a command for every (normal) file in the directory. If you want to make an array yourself, instead of using the lines of a program, that's

for i in {alpha,beta,gamma}; do
  echo item: $i
done

To get a list of files to which you know the extension and want to omit it in the loop, try

for i in $( ls *.png | sed "s/\\.png$//" ); do
...

for .png files. Incidentally, you can strip an extension off of a filename in a variable by doing

$( echo -n $FILENAME | sed "s/\\.png$//" )

Startup files

See shell startup scripts for what to put in these files.

You generally have a choice between .bashrc (see also rc file), .bash_profile, .profile, /etc/profile, et cetera. Here's the gist of what the man basher has to say:

As a login shell

Running as a login shell, bash will always source /etc/profile (if it exists). Then, it selects the first readable file in this order and sources it:

  • ~/.bash_profile
  • ~/.bash_login
  • ~/.profile

Starting login bash with --noprofile makes it skip both /etc/profile and the local profiles.

Incidentally, bash will source ~/.bash_logout upon your logout if it exists.

As a non-login shell

Running as a non-login shell, bash will run ~/.bashrc if it exists. --norc will make it skip the rc file altogether; --rcfile filename will make it source another file instead of ~/.bashrc.