How to Execute Script in The Current Shell on Linux

How to execute bash script in same shell

First, you will need to save save the command in a scriptfile, for example Myscript.sh.

Second, you can execute the script file to run your commands simultaneously using

. ./Myscript.sh

The first . stands for current shell and the second . for current directory.

Execute a shell script in current shell with sudo permission

What you are trying to do is impossible; your current shell is running under your regular user ID (i.e. without root the access sudo would give you), and there is no way to grant it root access. What sudo does is create a new *sub*process that runs as root. The subprocess could be just a regular program (e.g. sudo cp ... runs the cp program in a root process) or it could be a root subshell, but it cannot be the current shell.

(It's actually even more impossible than that, because the sudo command itself is executed as a subprocess of the current shell -- meaning that in a sense it's already too late for it to do anything in the "current shell", because that's not where it executes.)

execute a line from a script in the current shell environment

You could combine your sed command with a process substitution and source it:

source <(sed -n '4p' script.zsh)

though you might want to use a pattern match for the print line in case the line numbers shift.

source <(sed -n '/^MAC=/p' script.zsh)

Execute the shell script that is in the current directory, not the one in $PATH

If you are working in a script, then you can define your environment variables at the start of it, like:

MY_VAR="/my/path/to/scripts"

And then call your script from within that script using:

${MY_VAR}/my_script.sh

Kind of the same as running the script using its full path.

I hope this helps!


Edit 1: - If you want to run a specific script as you mentioned in the comments, under a specific directory, you can try changing directories to the one that contains the file, and then running the script like ./script1.sh.

cd /path/containing/your/scripts/
./script1.sh

Edit 2: - Maybe you can create a wrapper for your script1.sh, placing the following if-block in there to check if the script is under the current directory or not.

if [ -e script1.sh ]; then
./script1.sh # runs the script under your current directory
else
script1.sh # runs the script searching for it in $PATH first
fi

How to execute the output of a command within the current shell?

$ ls | sed ... | source /dev/stdin

UPDATE: This works in bash 4.0, as well as tcsh, and dash (if you change source to .). Apparently this was buggy in bash 3.2. From the bash 4.0 release notes:

Fixed a bug that caused `.' to fail to read and execute commands from non-regular files such as devices or named pipes.

How can I run a function from a script in command line?

If the script only defines the functions and does nothing else, you can first execute the script within the context of the current shell using the source or . command and then simply call the function. See help source for more information.

Shell script current directory?

The current(initial) directory of shell script is the directory from which you have called the script.

Do bash scripts execute in new shells or subshells?

You're correct; when you run a script with ./shell.sh, it runs in a new shell, not a subshell of the current shell.

It does run in a subprocess, which is a shell, so it's a tempting and common mistake to say "subprocess+shell=subshell, so it must be a subshell!" But that's incorrect. The shell running the script won't inherit shell variables from the parent shell process (it'll inherit environment variables, i.e. exported variables, but that's true of any subprocess), it won't inherit shell modes (e.g. set -e) or other shell state, and it won't even necessarily be running the same shell (if you're running bash and the script has a #!/bin/zsh shebang, it'll run in zsh). So it's logically a different shell that just happens to be running as a subprocess of the shell that launched it.



Related Topics



Leave a reply



Submit