Sample Header Ad - 728x90

Unix & Linux Stack Exchange

Q&A for users of Linux, FreeBSD and other Unix-like operating systems

Latest Questions

1 votes
2 answers
282 views
Why does the command 'source' have that name?
I wonder why a command that *executes commands from a file in the current shell* is named `source`. I can't see a relation between **run commands in the current shell** and the meaning of the english word **source**. Is there a history behind that name?
I wonder why a command that *executes commands from a file in the current shell* is named source. I can't see a relation between **run commands in the current shell** and the meaning of the english word **source**. Is there a history behind that name?
rrd (145 rep)
Jul 12, 2019, 01:48 PM • Last activity: Jan 9, 2025, 12:46 PM
16 votes
4 answers
23272 views
How can I skip the rest of a script without exiting the invoking shell, when sourcing the script?
I have a bash script, where I call `exit` somewhere to skip the rest of the script when `getopts` doesn't recognize an option or doesn't find an expected option argument. while getopts ":t:" opt; do case $opt in t) timelen="$OPTARG" ;; \?) printf "illegal option: -%s\n" "$OPTARG" >&2 echo "$usage" >...
I have a bash script, where I call exit somewhere to skip the rest of the script when getopts doesn't recognize an option or doesn't find an expected option argument. while getopts ":t:" opt; do case $opt in t) timelen="$OPTARG" ;; \?) printf "illegal option: -%s\n" "$OPTARG" >&2 echo "$usage" >&2 exit 1 ;; :) printf "missing argument for -%s\n" "$OPTARG" >&2 echo "$usage" >&2 exit 1 ;; esac done # reset of the script I source the script in a bash shell. When something is wrong, the shell exits. Is there some way other than exit to skip the rest of the script but without exiting the invoking shell? Replacing exit with return doesn't work like for a function call, and the rest of the script will runs. Thanks.
Tim (106420 rep)
Aug 2, 2018, 02:45 PM • Last activity: Nov 15, 2023, 11:39 PM
-1 votes
1 answers
298 views
Can a bash script add to the context of zsh?
I have recently migrated from bash to zsh. I used to use the following `token_refresh.sh` script to `ssh-add` my private key to the session: #!/usr/bin/env bash echo "configuring ssh access.." eval "$(ssh-agent -s)" ssh-add ./my-key However, I can't run this directly from zsh in the dir /ssh: $ ls m...
I have recently migrated from bash to zsh. I used to use the following token_refresh.sh script to ssh-add my private key to the session: #!/usr/bin/env bash echo "configuring ssh access.." eval "$(ssh-agent -s)" ssh-add ./my-key However, I can't run this directly from zsh in the dir /ssh: $ ls my-key my-key.pub token_refresh.sh $ . token_refresh.sh .: no such file or directory: token_refresh.sh I can run bash scripts using bash token_refresh.sh However, doesn't that start a bash session, ssh-add the key to the bash session, then exits. How can I portably run this script in bash and zsh?
StuperUser (183 rep)
May 15, 2023, 10:26 AM • Last activity: May 15, 2023, 11:23 AM
1 votes
1 answers
25770 views
/bin/ksh: bad interpreter: No such file or directory
I have script with `#!/bin/ksh` in the first line. When I try to execute this script (run `./myscript.sh`) the error occurred: -bash: ./myscript.sh: /bin/ksh: bad interpreter: No such file or directory But when I execute this script through `source myscript.sh` or `bash myscript.sh` command - script...
I have script with #!/bin/ksh in the first line. When I try to execute this script (run ./myscript.sh) the error occurred: -bash: ./myscript.sh: /bin/ksh: bad interpreter: No such file or directory But when I execute this script through source myscript.sh or bash myscript.sh command - script runs successfully. Yes, ksh is not installed and it is correct to install this. But I can't understand different behavior ./ and bash or source
Nik (13 rep)
Sep 12, 2019, 12:59 PM • Last activity: Sep 12, 2019, 02:01 PM
1 votes
2 answers
980 views
How to decrypt the file on the fly without process substitution?
I need to source the file by decrypting it on the fly using the below command. . <(gpg -qd "$encrypted_filename") sh is not supporting process substitution. I can't use bash. Please suggest some other way. https://unix.stackexchange.com/questions/237939/is-there-a-way-to-source-an-encrypted-gpg-file...
I need to source the file by decrypting it on the fly using the below command. . <(gpg -qd "$encrypted_filename") sh is not supporting process substitution. I can't use bash. Please suggest some other way. https://unix.stackexchange.com/questions/237939/is-there-a-way-to-source-an-encrypted-gpg-file-on-the-fly-in-a-script
aravindderajan (11 rep)
Nov 21, 2017, 01:22 PM • Last activity: Jun 21, 2018, 08:03 PM
6 votes
3 answers
3344 views
Parameters passed to a sourced script are wrong
A script on AIX which checks the number of parameters passed and complains if they are not correct:- if [ "$#" -ge 1 ]; then ... else echo 'Usage: myscript [b] [c]' fi The script sets some environment variables so it is sourced. By that I mean I type the following on my command line. There is no sec...
A script on AIX which checks the number of parameters passed and complains if they are not correct:- if [ "$#" -ge 1 ]; then ... else echo 'Usage: myscript [b] [c]' fi The script sets some environment variables so it is sourced. By that I mean I type the following on my command line. There is no second script, I am not sourcing a script from within another script. . ./myscript.sh If I pass it no parameters, I expect it to print out the usage statement but instead it runs using the parameters passed to the previous command that was sourced (not necessarily even this script!). To double check this I added this line at the beginning of my script to prove that this was the case. echo $# $@ If I pass it no parameters, it prints out the number and the list of parameters from a previously sourced script. How do I get it to say $# is zero when I pass it zero parameters and source it? The smallest possible recreate is a script thus:- **myscript.sh** echo $# $@ and then run it thus:- . ./myscript.sh a b c which prints out 3 a b c then run it again without any parameters thus:- . ./myscript.sh which prints out the same 3 a b c when you would expect it to print out 0 If you don't source the script and just run it thus:- ./myscript.sh then it works as expected and prints out 0 I hope that clarifies the issue at hand. Since I seem to be getting lots of answers to explain why the above doesn't work and no answers to tell me what I should do instead, I thought I would have one more attempt at trying to explain what I am trying to do. **Requirement** I need to write a script that expects at least one parameter and complains if it is not invoked with a said parameter (see the first block of code in my question above), and this script needs to set some environment variables. Because of this second requirement I have assumed that I need to source the script so that the environment variables are still set when the script completes. How can you write a script that can tell when it has not been passed parameters that is also able to set an environment variable?
Morag Hughson (171 rep)
May 3, 2018, 11:09 AM • Last activity: May 7, 2018, 07:51 AM
Showing page 1 of 6 total questions