2

I want to add a shell command in my bash shell script so that when the script is executed, it does an action equivalent to pressing enter key on the keyboard and then execute the rest of the commands in the script

eg : script.sh

#! /bin/bash <command equivalent to pressing enter key> echo "hi" 

What should be present in between the angle brackets so that output should be

$sh script.sh $ hi $ 
5
  • 1. The shebang line needs to be #!/bin/bash 2. The prompt is never shown when executing a shell script unless said script explicitly calls a shell. How exactly are you going to use this? Commented Jan 5, 2011 at 11:31
  • I tried adding the first line in the script as "#!/bin/bash 2" and then executed the command. But it just prints "hi" without showing the prompt Commented Jan 5, 2011 at 11:43
  • What you're asking for doesn't make sense: the prompt says “hello user, you may now type a new command, the previous command has finished”. What are you trying to accomplish? Commented Jan 5, 2011 at 19:43
  • 1
    @InsDel: A space is allowed after #!. Commented Jan 5, 2011 at 19:44
  • The '1.' and '2.' were enumerating list items. @Gilles: Well, learn something new every day. XD I'm just used to seeing it without the space though. Commented Jan 5, 2011 at 20:06

2 Answers 2

2

On Windows;

echo. echo hi echo. 

On Linux;

echo echo hi echo 
Sign up to request clarification or add additional context in comments.

3 Comments

Well, try it out anyway, it might work :D. Shell scripting is very similar between platforms.
@ Christian Sciberras : I tried it, but i am afraid, it does not work. Instead it says "Syntax Error"
I meant to emulate the behavior on pressing enter key on the shell commad prompt. I believe what u mentioned is not what i want. Hope i made it clear. Thanks a ton for ur quick help anyways :)
1

Use the 'read' command:

#!/bin/bash read x echo "hi" 

5 Comments

The way I understood him, he wanted a blank line, not waiting for user input.
I understood he simply wanted the user to "hit enter to continue".
@ Christian Sciberras: When i press enter through shell script, i expect to see the command promt before running the rest of the command in the script
You could add echo "\$" before the 'read' line, but it won't be the actual command prompt from your shell - it will be a fake.
Well you could try "echo $PS", but again you are really faking the command prompt. What you are asking requires the shell script to be launched in the background but still have access to the controlling tty - very tricky (but possible I reckon). Can you tell me why you require the original command prompt?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.