Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8962969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:15:23+00:00 2026-06-15T16:15:23+00:00

What is the difference between sh and source ? source: source filename [arguments] Read

  • 0

What is the difference between sh and source?

source: source filename [arguments]
    Read and execute commands from FILENAME and return.  The pathnames
    in $PATH are used to find the directory containing FILENAME.  If any
    ARGUMENTS are supplied, they become the positional parameters when
    FILENAME is executed.

And for man sh:

NAME
       bash - GNU Bourne-Again SHell

SYNOPSIS
       bash [options] [file]

COPYRIGHT
       Bash is Copyright (C) 1989-2004 by the Free Software Foundation, Inc.

DESCRIPTION
       Bash  is  an sh-compatible command language interpreter that executes commands read from the standard input or from a file.  Bash also incorporates
       useful features from the Korn and C shells (ksh and csh).

       Bash is intended to be a conformant implementation of the IEEE POSIX Shell and Tools specification (IEEE Working Group 1003.2).
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:15:24+00:00Added an answer on June 15, 2026 at 4:15 pm

    When you call source or . (the one is an alias to the other. source cmd not POSIX – kind of bashism), you load and execute a shell script into the current shell process. So you can

    • read variables set in the sourced script,
    • use functions defined within it.
    • and even execute forks and/or subprocess if script do this.

    When you call sh, you initiate a fork (sub-process or child) that runs a new session of /bin/sh (which is often a symbolic link to bash). In this case, environment variables set by the sub-script would be dropped when the sub-script terminate.

    Caution: sh could be a symlink to another shell.

    Practical sample

    For example, if you want to change current working directory by a specific manner, you could not do

    $ cat <<eof >myCd2Doc.sh
    #!/bin/sh
    cd /usr/share/doc
    eof
    
    $ chmod +x myCd2Doc.sh
    

    This won’t do what you expect:

    $ cd /tmp
    $ pwd
    /tmp
    $ ~/myCd2Doc.sh
    $ pwd
    /tmp
    

    because current working dir is part of environment and myCd2Doc.sh would run in a subshell.

    But:

    $ source ~/myCd2Doc.sh
    $ pwd
    /usr/share/doc
    

    Same, for declaring a function:

    $ cat >~/myCd2Doc.source <<eof
    # Shell source file
    myCd2Doc() {
        cd /usr/share/doc
    }
    eof
    
    $ . ~/myCd2Doc.source
    $ cd /tmp
    $ pwd
    /tmp
    $ myCd2Doc
    $ pwd
    /usr/share/doc
    

    Have a look at mycd function!! (With bash completion based on Associative Array).

    Execution level $SHLVL

    $ cd /tmp
    printf %b '\43\41/bin/bash\necho This is level \44SHLVL.\n' >qlvl.sh
    
    $ bash qlvl.sh 
    This is level 2.
    
    $ source qlvl.sh 
    This is level 1.
    

    Recursion (when a script run from itself)

    $ cat <<"eoqlvl2" >qlvl2.sh 
    #!/bin/bash
    
    export startLevel recursionLimit=5
    echo This is level $SHLVL started:${startLevel:=$SHLVL}.
    (( SHLVL < recursionLimit )) && ./qlvl2.sh
    eoqlvl2
    $ chmod +x qlvl2.sh
    
    $ ./qlvl2.sh 
    This is level 2 started:2.
    This is level 3 started:2.
    This is level 4 started:2.
    This is level 5 started:2.
    
    $ source qlv2.sh 
    This is level 1 started:1.
    This is level 2 started:1.
    This is level 3 started:1.
    This is level 4 started:1.
    This is level 5 started:1.
    

    A little futher

    $ sed '$a ps --sid $SID fw' qlvl.sh >qlvl3.sh
    $ chmod +x qlvl3.sh 
    $ export SID
    $ read SID < <(ps ho sid $$)
    $ echo $SID $$
    8983 8983
    

    ( Current PID ($$ == process Id) are same identifier than SID (session ID). It’s not alway true.)

    $ ./qlvl3.sh 
    This is level 2.
      PID TTY      STAT   TIME COMMAND
     8983 pts/10   Ss     0:00 /bin/bash
    10266 pts/10   S+     0:00  \_ /bin/bash ./qlvl3.sh
    10267 pts/10   R+     0:00      \_ ps --sid 8983 fw
    
    $ . qlvl3.sh 
    This is level 1.
      PID TTY      STAT   TIME COMMAND
     8983 pts/10   Ss     0:00 /bin/bash
    10428 pts/10   R+     0:00  \_ ps --sid 8983 fw
    

    Dot . is an alias of source. So the only difference between two command are slash replaced by space.

    And a final test:

    $ printf %b '\43\41/bin/bash\necho Ending this.\nsle' \
        'ep 1;exit 0\n' >finalTest.sh
    
    $ bash finalTest.sh 
    Ending this.
    
    $ source finalTest.sh
    Ending this.
    

    … You may notice a different behaviour between the two syntaxes. 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently tried to find a good source on the difference between monads
I can't work out from looking through the source what the difference is between
I didn't think there was a difference between an inputstream object read from a
What is the difference between Source files and Include path in a PHP project
What is the difference between these two RTC source control icons ? Is it
The difference between Chr and Char when used in converting types is that one
What's the difference between source file and translation unit?
What is the difference between Source and Generated Source in Firefox? Please explain with
What is the difference between these two in a source file? namespace { int
What is the difference between custom building a CMS website and an open source

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.