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 8821221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:47:54+00:00 2026-06-14T05:47:54+00:00

I have a shell script that I would like to test with shUnit. The

  • 0

I have a shell script that I would like to test with shUnit. The script (and all the functions) are in a single file since it makes installation much easier.

Example for script.sh

#!/bin/sh

foo () { ... }
bar () { ... }

code

I wanted to write a second file (that does not need to be distributed and installed) to test the functions defined in script.sh

Something like run_tests.sh

#!/bin/sh

. script.sh

# Unit tests

Now the problem lies in the . (or source in Bash). It does not only parse function definitions but also executes the code in the script.

Since the script with no arguments does nothing bad I could

. script.sh > /dev/null 2>&1

but I was wondering if there is a better way to achieve my goal.

Edit

My proposed workaround does not work in the case the sourced script calls exit so I have to trap the exit

#!/bin/sh

trap run_tests ERR EXIT

run_tests() {
   ...
}

. script.sh

The run_tests function is called but as soon as I redirect the output of the source command the functions in the script are not parsed and are not available in the trap handler

This works but I get the output of script.sh:

#!/bin/sh
trap run_tests ERR EXIT
run_tests() {
   function_defined_in_script_sh
}
. script.sh

This does not print the output but I get an error that the function is not defined:

#!/bin/sh
trap run_tests ERR EXIT
run_tests() {
   function_defined_in_script_sh
}
. script.sh | grep OUTPUT_THAT_DOES_NOT_EXISTS

This does not print the output and the run_tests trap handler is not called at all:

#!/bin/sh
trap run_tests ERR EXIT
run_tests() {
   function_defined_in_script_sh
}
. script.sh > /dev/null
  • 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-14T05:47:56+00:00Added an answer on June 14, 2026 at 5:47 am

    According to the “Shell Builtin Commands” section of the bash manpage, . aka source takes an optional list of arguments which are passed to the script being sourced. You could use that to introduce a do-nothing option. For example, script.sh could be:

    #!/bin/sh
    
    foo() {
        echo foo $1
    }
    
    main() {
        foo 1
        foo 2
    }
    
    if [ "${1}" != "--source-only" ]; then
        main "${@}"
    fi
    

    and unit.sh could be:

    #!/bin/bash
    
    . ./script.sh --source-only
    
    foo 3
    

    Then script.sh will behave normally, and unit.sh will have access to all the functions from script.sh but will not invoke the main() code.

    Note that the extra arguments to source are not in POSIX, so /bin/sh might not handle it—hence the #!/bin/bash at the start of unit.sh.

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

Sidebar

Related Questions

I would like to have a shell script that copies some logs from a
I have a shell script that I would like to run at the end
I have a Korn shell script that I would like to change a variable
I have a Bourne Shell script that has several functions in it, and allows
I have a shell script file (run.sh) that contains the following: #!/bin/bash %JAVA_HOME%/bin/java -jar
I have found a useful shell script that shows all files in a directory
I have created a shell .command on Mac OS X that I would like
I have a Bash shell script that invokes a number of commands. I would
I have a simple Linux shell script that parses a file generated in windows
Have a shell script that reads the files in a particular directory. #!/bin/bash for

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.