Interview › Scripting (Bash, Groovy)
What does sourcing a script do differently from executing it? [Basic]
Answer
Sourcing runs the file in the current shell instead of starting a separate shell process. That means changes to variables, functions, shell options, and the working directory persist after the sourced file completes.
Technical explanation
It is useful for loading configuration or helper functions into an interactive session or another script.
It is not isolated, so a bad sourced script can overwrite variables, change set options, or exit the caller unless carefully written.
For libraries, avoid top-level destructive commands and expose functions that the caller can invoke deliberately.
Hands-on example
cat > lib.sh <<'EOF'
log() { printf '[%s] %s\n' "$(date +%H:%M:%S)" "$*"; }
export TOOL_REGION=ap-south-1
EOF
source ./lib.sh
log "region is $TOOL_REGION"
Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.
More Scripting (Bash, Groovy) interview questions
- What is the purpose of the shebang line, and what does #!/bin/bash do? [Basic]
- What is the difference between sh and bash? [Basic]
- How do you make a script executable and run it? [Basic]
- What is the difference between running a script with ./script.sh, bash script.sh, and source script.sh? [Basic]
- How do you declare a variable in Bash, and why are spaces around = not allowed? [Basic]
- What is the difference between $var and ${var}? [Basic]
- What is the difference between single quotes and double quotes in Bash? [Basic]
- Why should you quote variables, and what bug does unquoting cause? [Basic]