Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
scripting:bashbehaviour [2011/06/04 14:56]
thebonsai crosslink variables
scripting:bashbehaviour [2015/08/04 10:03] (current)
thebonsai [Interactive shell] still typo DOH!
Line 12: Line 12:
  
  
-Bash acting as a "login shell" ​is reads and sets (executes) the user's profile from the profiles ​''/​etc/​profile''​ and one of ''​~/​.bash_profile'',​ ''​~/​.bash_login'', ​and ''​~/​.profile''​ (in that order, ​taking ​the first one that's readable!).+As a "login shell", Bash reads and sets (executes) the user's profile from ''/​etc/​profile''​ and one of ''​~/​.bash_profile'',​ ''​~/​.bash_login'', ​or ''​~/​.profile''​ (in that order, ​using the first one that's readable!).
  
 When a login shell exits, Bash reads and executes commands from the file ''​~/​.bash_logout'',​ if it exists. When a login shell exits, Bash reads and executes commands from the file ''​~/​.bash_logout'',​ if it exists.
  
-Why an extra login shell mode? There are many actions and variable sets that only make sense for the initial user login. That's why all UNIX(r) shells have (should have) an extra "​login"​ mode.+Why an extra login shell mode? There are many actions and variable sets that only make sense for the initial user login. That's why all UNIX(r) shells have (should have) "​login"​ mode.
  
 __**Methods to start Bash as a login shell:**__ __**Methods to start Bash as a login shell:**__
-  * the first character of ''​argv[0]''​ is ''​-''​ (a hyphen): traditional UNIX(r) ​shell start from the ''​login''​ binary+  * the first character of ''​argv[0]''​ is ''​-''​ (a hyphen): traditional UNIX(r) ​shells ​start from the ''​login''​ binary
   * Bash is started with the ''​-l''​ option   * Bash is started with the ''​-l''​ option
   * Bash is started with the ''<​nowiki>​--login</​nowiki>''​ option   * Bash is started with the ''<​nowiki>​--login</​nowiki>''​ option
Line 35: Line 35:
 ==== Interactive shell ==== ==== Interactive shell ====
  
-When Bash starts as an interactive ​(non-login!) shell, it reads and executes commands from ''​~/​.bashrc''​. This files should contain for example aliases, since they need to be defined in every shell as they'​re not inherited from the parent shell.+When Bash starts as an interactive non-login shell, it reads and executes commands from ''​~/​.bashrc''​. This file should containfor examplealiases, since they need to be defined in every shell as they'​re not inherited from the parent shell.
  
-The feature to have a system-wide ''/​etc/​bash.bashrc''​ or a similar system-wide ​RC-File is specific to vendors and distributors that ship //an own, patched variant of Bash//. The classic way to have a system-wide ​RC file is to e.g. ''​source /​etc/​bashrc''​ from every user's ''​~/​.bashrc''​.+The feature to have a system-wide ''/​etc/​bash.bashrc''​ or a similar system-wide ​rc-file is specific to vendors and distributors that ship //their own, patched variant of Bash//. The classic way to have a system-wide ​rc file is to ''​source /​etc/​bashrc''​ from every user's ''​~/​.bashrc''​.
  
 __**Methods to test for interactive-shell mode:**__ __**Methods to test for interactive-shell mode:**__
Line 54: Line 54:
 When Bash starts in SH compatiblity mode, it tries to mimic the startup behaviour of historical versions of ''​sh''​ as closely as possible, while conforming to the POSIX(r) standard as well. The profile files read are ''/​etc/​profile''​ and ''​~/​.profile'',​ if it's a login shell. When Bash starts in SH compatiblity mode, it tries to mimic the startup behaviour of historical versions of ''​sh''​ as closely as possible, while conforming to the POSIX(r) standard as well. The profile files read are ''/​etc/​profile''​ and ''​~/​.profile'',​ if it's a login shell.
  
-If it's not a login shell, the environment variable [[syntax:​shellvars#​ENV|ENV]] is evaluated and the resulting filename is taken as name of the startup file.+If it's not a login shell, the environment variable [[syntax:​shellvars#​ENV|ENV]] is evaluated and the resulting filename is used as the name of the startup file.
  
 After the startup files are read, Bash enters the [[#​posix_run_mode | POSIX(r) compatiblity mode (for running, not for starting!)]]. After the startup files are read, Bash enters the [[#​posix_run_mode | POSIX(r) compatiblity mode (for running, not for starting!)]].
  
 __**Bash starts in ''​sh''​ compatiblity mode when:**__ __**Bash starts in ''​sh''​ compatiblity mode when:**__
-  * the base filename in ''​argv[0]''​ is ''​sh''​ (:!: Attention dear uber-clever Linux users... ​''/​bin/​sh''​ may be linked to ''/​bin/​bash'',​ but that doesn'​t mean it acts like ''/​bin/​bash''​ :!:)+  * the base filename in ''​argv[0]''​ is ''​sh''​ (:!: NB: ''/​bin/​sh''​ may be linked to ''/​bin/​bash'',​ but that doesn'​t mean it acts like ''/​bin/​bash''​ :!:)
  
  
Line 79: Line 79:
 ==== Quick startup file reference ==== ==== Quick startup file reference ====
  
-  * Eventual system-wide ​RC-Files are usually read when ''​~/​.bashrc''​ would be read (at least Debian GNU/Linux behaves like that)+  * Eventual system-wide ​rc-files are usually read when ''​~/​.bashrc''​ would be read (at least Debian GNU/Linux behaves like that)
   * Regardless of the system-wide files in ''/​etc''​ which are always read, Bash usually reads the first file found, when multiple choices are given (for  user files in ''​~/''​)   * Regardless of the system-wide files in ''/​etc''​ which are always read, Bash usually reads the first file found, when multiple choices are given (for  user files in ''​~/''​)
  
Line 103: Line 103:
 ==== POSIX run mode ==== ==== POSIX run mode ====
  
-In POSIX(r) mode, Bash follows the POSIX(r) standard regarding behaviour and parsing (include of a Bash maintainer'​s document):+In POSIX(r) mode, Bash follows the POSIX(r) standard regarding behaviour and parsing (excerpt from a Bash maintainer'​s document):
 <​code>​ <​code>​
 Starting Bash with the `--posix'​ command-line option or executing `set Starting Bash with the `--posix'​ command-line option or executing `set
Line 113: Line 113:
 files. files.
  
-The following ​list is what's changed when `POSIX mode' ​is in effect:+The following ​lists what's changed when Bash is in `POSIX mode':
  
   1. When a command in the hash table no longer exists, Bash will   1. When a command in the hash table no longer exists, Bash will
Line 165: Line 165:
      the redirection.      the redirection.
  
- 16. Function names must be valid shell `name'​s.  That is, they may not+ 16. Function names must be valid shell names.  That is, they may not
      ​contain characters other than letters, digits, and underscores,​ and      ​contain characters other than letters, digits, and underscores,​ and
      may not start with a digit. ​ Declaring a function with an invalid      may not start with a digit. ​ Declaring a function with an invalid
Line 177: Line 177:
      the POSIX standard, and include things like passing incorrect      the POSIX standard, and include things like passing incorrect
      ​options,​ redirection errors, variable assignment errors for      ​options,​ redirection errors, variable assignment errors for
-     ​assignments preceding the command name, and so on.+     ​assignments preceding the command name, etc.
  
  19. If `CDPATH'​ is set, the `cd' builtin will not implicitly append  19. If `CDPATH'​ is set, the `cd' builtin will not implicitly append
Line 298: Line 298:
  
   * the ''​cd''​ command can't be used to change directories   * the ''​cd''​ command can't be used to change directories
-  * the variables ​''​SHELL''​''​PATH''​''​ENV'' ​and ''​BASH_ENV'' ​can't be set or unset+  * the variables ​[[syntax:​shellvars#​SHELL|SHELL]][[syntax:​shellvars#​PATH|PATH]][[syntax:​shellvars#​ENV|ENV]] ​and [[syntax:​shellvars#​BASH_ENV|BASH_ENV]] can't be set or unset
   * command names that contain a ''/''​ (slash) can't be called (hence you're limited to ''​PATH''​)   * command names that contain a ''/''​ (slash) can't be called (hence you're limited to ''​PATH''​)
   * filenames containing a ''/''​ (slash) can't be specified as argument to the ''​source''​ or ''​.''​ builtin command   * filenames containing a ''/''​ (slash) can't be specified as argument to the ''​source''​ or ''​.''​ builtin command
   * filenames containing a ''/''​ (slash) can't be specified as argument to the ''​-p''​ option of the ''​hash''​ builtin command   * filenames containing a ''/''​ (slash) can't be specified as argument to the ''​-p''​ option of the ''​hash''​ builtin command
   * function definitions are not inherited from the environment at shell startup   * function definitions are not inherited from the environment at shell startup
-  * the environment variable ​''​SHELLOPTS'' ​is ignored at startup+  * the environment variable ​[[syntax:​shellvars#​SHELLOPTS|SHELLOPTS]] ​is ignored at startup
   * redirecting output using the ''>'',​ ''>​|'',​ ''<>'',​ ''>&'',​ ''&>'',​ and ''<​nowiki>>></​nowiki>''​ redirection operators isn't allowed   * redirecting output using the ''>'',​ ''>​|'',​ ''<>'',​ ''>&'',​ ''&>'',​ and ''<​nowiki>>></​nowiki>''​ redirection operators isn't allowed
   * the ''​exec''​ builtin command can't replace the shell with another process   * the ''​exec''​ builtin command can't replace the shell with another process
Line 311: Line 311:
   * turning off restricted mode with ''​set +r''​ or ''​set +o restricted''​ is (of course) forbidden   * turning off restricted mode with ''​set +r''​ or ''​set +o restricted''​ is (of course) forbidden
  
-When command ​is ran that's a shell script, and Bash is in charge to run that shell script, then the restrictions are **turned off** for the (sub-)shell that runs that shellscript.\\ +The "​-r"​ restrictions are turned on **after** Bash has read its startup files. 
-The restrictions are turned on **after** Bash read the startup files.+ 
 +When the command that is run is a shell script, then the restrictions are **turned off** for the (sub-)shell that runs that shell script
  
 __**The restricted shell can be switched on by:**__ __**The restricted shell can be switched on by:**__
  • scripting/bashbehaviour.1307199391.txt
  • Last modified: 2011/06/04 14:56
  • by thebonsai