Monday, August 15, 2016

Invoking bash

  • Syntax:

bash [long-opt] [-ir] [-abefhkmnptuvxdBCDHP] [-o option] [-O shopt_option] [argument ...]
bash [long-opt] [-abefhkmnptuvxdBCDHP] [-o option] [-O shopt_option] -c string [argument ...]
bash [long-opt] -s [-abefhkmnptuvxdBCDHP] [-o option] [-O shopt_option] [argument ...]

  • All of the single-character options used with the set builtin can be used as options when the shell is invoked.
This refers to the list [-abefhkmnptuvxdBCDHP] in the above example.

  • In addition, there are several multi-character options that you can use. These options must appear on the command line before the single-character options to be recognized. This refers to the  [long-opt] which are explained below

--help
Display a usage message on standard output and exit successfully.

/export/home/sm017r$bash --help
GNU bash, version 3.2.57(1)-release-(sparc-sun-solaris2.10)
Usage:  bash [GNU long option] [option] ...
        bash [GNU long option] [option] script-file ...
GNU long options:
        --debug
        --debugger
        --dump-po-strings
        --dump-strings
        --help
        --init-file
        --login
        --noediting
        --noprofile
        --norc
        --posix
        --protected
        --rcfile
        --restricted
        --verbose
        --version
        --wordexp
Shell options:
        -irsD or -c command or -O shopt_option          (invocation only)
        -abefhkmnptuvxBCHP or -o option
Type `bash -c "help set"' for more information about shell options.
Type `bash -c help' for more information about shell builtin commands.
Use the `bashbug' command to report bugs.
/export/home/sm017r$

--init-file filename
--rcfile filename
Execute commands from filename (instead of ~/.bashrc) in an interactive shell.
--login
Equivalent to -l. This makes the shell behave as a login shell (which makes login start up scripts to be executed)

--noediting
Do not use the gnu Readline library to read command lines when the shell is interactive.
--noprofile
Don’t load the system-wide startup file /etc/profile or any of the personal initialization files ~/.bash_profile, ~/.bash_login, or ~/.profile when Bashis invoked as a login shell.

--norc
Don’t read the ~/.bashrc initialization file in an interactive shell. This is on by default if the shell is invoked as sh.
--posix
Change the behavior of Bash where the default operation differs from the posix standard to match the standard.
This is intended to make Bash behave as a strict superset of that standard.

--restricted
Make the shell a restricted shell
--verbose
Equivalent to -v. Print shell input lines as they’re read.
This is similar to use set -v

This prints every line that bash is executing.

[sukul1@cldi016 ~]$ bash --verbose script1.bash
module () {  eval `/usr/bin/modulecmd bash $*`
}
#! /usr/bin/bash
echo "che"
che
echo $PATH
/opt/teradata/client/15.10/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/sm017r/bin
[sm017r@cldi016 ~]$

[sukul1@cldi016 ~]$ bash script1.bash
che
/opt/teradata/client/15.10/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/sm017r/bin
[sm017r@cldi016 ~]$


--version
Show version information for this instance of Bash on the standard output and  exit successfully.

[sm017r@cldi016 ~]$ bash --version
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
[sm017r@cldi016 ~]$


  • There are several single-character options that may be supplied at invocation which are not available with the set builtin.

-c
Read and execute commands from the first non-option argument after processing the options, then exit.
Any remaining arguments are assigned to the positional parameters, starting with $0.

[sukul1@lldd010 ~]$ bash -c ls| wc -l
6
[sukul1@lldd010 ~]$

-i
Force the shell to run interactively.
-l
Make this shell act as if it had been directly invoked by login. When the shell is interactive, this is equivalent to starting a login shell with ‘exec -l bash’.
When the shell is not interactive, the login shell startup files will be executed.
‘exec bash -l’ or ‘exec bash --login’ will replace the current shell with a Bash login shell.
This would cause the /etc/profile and .bash_profile to be executed.
-r
Make the shell a restricted shell
-s
If this option is present, or if no arguments remain after option processing, then commands are read from the standard input. This option allows the positional parameters to be set when invoking an interactive shell.

[sukul1@cldi016 ~]$ bash -s Sukul Mukul
[sukul1@cldi016 ~]$ echo $1
Sukul
[sukul1@cldi016 ~]$ echo $2
Mukul
[sukul1@cldi016 ~]$

We can see that we started a interactive shell by set positional parameters using -s option.
[-+]O [shopt_option]
shopt option is one of the shell options accepted by the shopt builtin

If shopt option is present, -O sets the value of that option; +O unsets it. If shopt option is not supplied, the names
and values of the shell options accepted by shopt are printed on the standard
output. If the invocation option is +O, the output is displayed in a format that may be reused as input.



--
A -- signals the end of options and disables further option processing.
Any arguments after the -- are treated as filenames and arguments.



  • A login shell is one whose first character of argument zero is ‘-’, or one invoked with the --login option.

[sukul1@cldi016 ~]$ echo $0
-bash
[sukul1@cldi016 ~]$

  • An interactive shell is one started without non-option arguments, unless -s is specified, without specifying the -c option, and whose input and output are both connected to terminals or one started with the -i option.

If arguments remain after option processing, and neither the -c nor the -s option has been supplied, the first argument is assumed to be the name of a file containing shell commands When Bash is invoked in this fashion, $0 is set to the name of the file, and the positional parameters are set to the remaining arguments. Bash reads and executes commands from this file, then exits. Bash’s exit status is the exit status of the last command executed in the script. If no commands are executed,the exit status is 0.


No comments:

Post a Comment