41

How do you run a shell script in a new terminal in Linux from a terminal like "start test.bat" in Windows, also it should be working in the console mode.

1
  • 1
    which window environment(s) do you need to support? Commented Nov 30, 2012 at 16:12

4 Answers 4

39

Here's a simple example to get you started:

To write a shell script, do this on your command prompt:

echo -e '#!/bin/sh\n echo "hello world"' > abc.sh

This writes:

#!/bin/sh
echo "hello world"

To a file called abc.sh

Next, you want to set it to executable by:

chmod +x abc.sh

Now, you can run it by:

./abc.sh

And you should see:

hello world

On your terminal.

To run it in a new terminal, you can do:

gnome-terminal -x ./abc.sh

or, if it's xterm:

xterm -e ./abc.sh

Here's a list of different terminal emulators.

Alternatively, you just run it in your current terminal, but background it instead by:

./abc.sh &
Sign up to request clarification or add additional context in comments.

10 Comments

it works fineThank you so much for your help, and i have a another doubt will all the types of linux os support gnome-terminal command?
@RaviKumar No, that is not in general true. It largely depends on what desktop environment that linux distro uses: en.wikipedia.org/wiki/List_of_terminal_emulators Quite often it's gnome-terminal or xterm
also i need the command that should be working when i use the linux machine in the telnet session. i hope there will be answer for this.
@RaviKumar I'm not quite sure what you mean.
Hi Sampson. The shell script I execute in another window needs to hang there. So my original sh file needs to continue to the next line of commands. How can I do that? Thanks!
|
7

I came here wanting to figure out how to make a script spawn a terminal and run it self in it, so for those who want to do that I figured out this solution:

if [ ! -t 0 ]; then # script is executed outside the terminal?
  # execute the script inside a terminal window with same arguments
  x-terminal-emulator -e "$0" "$@"
  # and abort running the rest of it
  exit 0
fi

Comments

5

As of January 2020, the -e and -x option in gnome-terminal still run properly but throw out the following warnings:

For -e:

# Option “-e” is deprecated and might be removed in a later version of gnome-terminal.

# Use “-- ” to terminate the options and put the command line to execute after it.

For -x:

# Option “-x” is deprecated and might be removed in a later version of gnome-terminal.

# Use “-- ” to terminate the options and put the command line to execute after it.

Based on that information above, I confirmed that you can run the following two commands without receiving any warning messages:

gnome-terminal -- /bin/sh -c '<your command>'
gnome-terminal -- ./<your script>.sh

I hope this helps anyone else presently having this issue :)

Comments

3

For gnome try this.

Replace ls with the command you want to run

gnome-terminal -x sh -c "ls|less"

I hope this is what you want

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.