Dr. Mark Humphrys

School of Computing. Dublin City University.

Online coding site: Ancient Brain

coders   JavaScript worlds

Search:

Free AI exercises


Further Shell



How to pass information to a program

In general, how to pass information to a program:

  1. Set environment variables. All programs can read these.

  2. Command-line arguments.
    Program icon - Properties.

  3. Pipe to the program, where the program expects to read from standard input.

  4. Configuration files.
    • Many applications have "Settings" (or Preferences or Options) where a user can customise the app.
    • Choices saved somewhere on disk.
    • On Linux, usually saved in .* files and directories in home directory.
    • Example: Firefox config files on Linux:
       $HOME/.mozilla/firefox/id/prefs.js 
      and on Windows:
       C:\Program Files\Mozilla Firefox\defaults\pref\firefox.js 

  5. Talk to the program while it is running. How to do this depends on the OS.




Set variable on same line as command

You can set an environment variable just for one command in bash.

$ var=value command args 

bash manual: "A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator."

The variable has that value inside the command, but does not retain the value in bash after the command exits.




What shells are installed?

DCU Linux default is bash shell, but there are others installed.

See what is installed:

ls -l /bin/*sh
On DCU Linux, something like:
-rwxr-xr-x 1 root root 1168776 Apr 18  2019 /bin/bash
-rwxr-xr-x 1 root root  121464 Jan 17  2019 /bin/dash
lrwxrwxrwx 1 root root       4 Apr 18  2019 /bin/rbash -> bash
lrwxrwxrwx 1 root root       3 Feb  5  2019 /bin/rzsh -> zsh
lrwxrwxrwx 1 root root       4 Jan 17  2019 /bin/sh -> dash
-rwxr-xr-x 1 root root  861568 Feb  5  2019 /bin/zsh 

To start using one as the command-line, just type its name.
"exit" to return.



The shebang line

The first line of a shell script should state which shell it is to run in, using syntax like:
#!/bin/shellname
This is called a "shebang" line.

For example:


Can have endless number of interpreters of source text. Can invent your own.



If no shebang line

It is usually possible to omit the shebang line, and your system will try (for example) to run the script in the simple Bourne shell sh.

But you cannot depend on this - exactly what happens depends on each system.
Your shell scripts may not be portable to different UNIX-family systems.

So it is good practice to define the shell in the first line.





Environment variables in parent and child processes

  1. Script cannot change parent environment:
    cd (change directory) in Shell script only has effect within that script.
    In general, environment variables changed within Shell script (such as "cwd" or "PWD" - current working directory) are only changed for the duration of that script.

  2. Use alias to change current process's environment variables:
    Instead of a Shell script, use an alias, which is a command-line text substitution.

  3. Script's variables are not usable by children (scripts called by that script) by default:
    You need to "export" them to make them available to children.
    Example: prog1:

    x=3
    export x
    echo "prog1 [$x]"
    prog2
    

    calls prog2:

    echo "prog2 [$x]"
    

    Try it with and without export.
    export will make variable x available to a script called by this script, and any scripts called by that script, etc., without any further exporting needed. Only a single export statement is needed (in the top parent script).

  4. Functions in the same script can access variables:
    Note that environment variables are by default available to functions within the same Shell script without exporting.



Caching and re-building the PATH

It would be quite an overhead to search all the directories in the PATH every time you type a command.
So some (all?) shells make a list of executable files in these directories (with the exception of ".") once, at login, or at the start of running a script, and then the shell caches that list in memory for future use.

This can cause some problems. e.g. You add a new program to your $HOME/bin directory, which is in the PATH. You then type the name of the program and it is not recognised. The solution is you need to re-build the cache.


How to re-build the cache of the PATH

  source .FILE
where .FILE is the config file where the path is defined.

If you do this a lot, you might like to put the following alias in .bashrc:

  alias redo="source $HOME/.bashrc"
or .cshrc:
  alias redo    source $home/.cshrc
And then, every time your PATH cache is out of date, you type:
  redo


CDPATH (jumping around disk)

CDPATH is very useful.
It is a quick way of jumping around the disk.

On bash, in .bashrc:
export CDPATH=$HOME:$HOME/public_html
On csh, in .cshrc:
set cdpath = ( $home $home/public_html )

Q. Do you need "." in the CDPATH as well?

If you have defined "cdpath" like above, then wherever you are on the disk, you can jump direct to a subdirectory off your home directory or off your web directory by just typing "cd (subdirectory)".

Other tools for jumping around disk:

Using tools like this, jumping around the disk and performing tasks on the command-line can actually be quicker than doing it through the File Manager.




Characters



ancientbrain.com      w2mind.org      humphrysfamilytree.com

On the Internet since 1987.      New 250 G VPS server.

Note: Links on this site to user-generated content like Wikipedia are highlighted in red as possibly unreliable. My view is that such links are highly useful but flawed.