You are not logged in.

#1 2011-06-20 10:29:12

gauthma
Member
Registered: 2010-02-16
Posts: 218
Website

[Solved] bash, sh, and the source command

This simple script:

!/bin/bash

CONFIG="config.conf"
[ -f "$CONFIG" ] && source "$CONFIG"

runs fine when ran like

$ ./t.sh

or

$ bash t.sh

but borks when ran like so:

$ ls -l
-rw-r--r-- 1 oscar oscar   31 Jun 20 11:21 config.conf
-rwxr-xr-x 1 oscar oscar   72 Jun 20 11:21 t.sh
$ sh t.sh 
t.sh: line 4: source: config.conf: file not found

As you can see from the ls output above, the config.conf file exists and is readable. Furthermore, I get the same result when using "." instead of "source". What intrigues is that /bin/sh is a sym link to /bin/bash (and /bin/bash is a binary.), so both bash and sh should produce the same result. Why is that not the case is the issue that puzzles me.

$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 May  8 02:46 /bin/sh -> bash

Last edited by gauthma (2011-06-20 10:55:52)

Offline

#2 2011-06-20 10:36:45

Spider.007
Member
Registered: 2004-06-20
Posts: 1,175

Re: [Solved] bash, sh, and the source command

From the manpage:

source filename [arguments]
              Read  and  execute  commands  from filename in the current shell
              environment and return the exit status of the last command  exe‐
              cuted from filename.  If filename does not contain a slash, file
              names in PATH are used to find the  directory  containing  file‐
              name.   The  file  searched  for in PATH need not be executable.
              When bash is  not  in  posix  mode,  the  current  directory  is
              searched  if no file is found in PATH.  If the sourcepath option
              to the shopt builtin command is turned  off,  the  PATH  is  not
              searched.   If any arguments are supplied, they become the posi‐
              tional parameters when  filename  is  executed.   Otherwise  the
              positional  parameters  are unchanged.  The return status is the
              status of the last command exited within the  script  (0  if  no
              commands  are  executed),  and false if filename is not found or
              cannot be read.

combined with:

If  bash  is  invoked  with  the name sh, it tries to mimic the startup
       behavior of historical versions of sh as  closely  as  possible,  while
       conforming  to the POSIX standard as well.

aka, bash invoked as sh puts it in POSIX mode, and in POSIX mode the current path isn't searched

Last edited by Spider.007 (2011-06-20 10:37:48)

Offline

#3 2011-06-20 10:54:11

gauthma
Member
Registered: 2010-02-16
Posts: 218
Website

Re: [Solved] bash, sh, and the source command

Thanks for the quick reply. I had no manpage for "source", and I didn't remember to look it up on the bash manpage :$

Offline

#4 2011-06-20 11:14:06

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [Solved] bash, sh, and the source command

gauthma wrote:

Thanks for the quick reply. I had no manpage for "source"

[karol@black ~]$ help source
source: source filename [arguments]
    Execute commands from a file in the current shell.
    
    Read and execute commands from FILENAME in the current shell.  The
    entries in $PATH are used to find the directory containing FILENAME.
    If any ARGUMENTS are supplied, they become the positional parameters
    when FILENAME is executed.
    
    Exit Status:
    Returns the status of the last command executed in FILENAME; fails if
    FILENAME cannot be read.

Offline

#5 2011-06-20 11:25:24

gauthma
Member
Registered: 2010-02-16
Posts: 218
Website

Re: [Solved] bash, sh, and the source command

karol wrote:
gauthma wrote:

Thanks for the quick reply. I had no manpage for "source"

[karol@black ~]$ help source
source: source filename [arguments]

I even did source --help, and I didn't remember this one. Ouch.

Last edited by gauthma (2011-06-20 11:43:48)

Offline

Board footer

Powered by FluxBB