0

I made a simple bash script to take the positional parameter direct with the execution like:

$ Test $1 $2

However, it will not run unless I add sh in front of it like $ sh Test $1 $2

I made other scripts with input after being executed and they all run fine without the sh in front as I have it in the environment.

My simple program is like

echo "my name is $1 I am $2 old."

Any idea how i can run it without the sh in front?

chepner
  • 497,756
  • 71
  • 530
  • 681
Sami-M
  • 57
  • 6

3 Answers3

3

Test needs to be executable

chmod +x Test

It also needs to be in a directory that's in your PATH. If it isn't, you need to be specific:

./Test "$1" "$2"
chepner
  • 497,756
  • 71
  • 530
  • 681
  • it is executable and it is also in the path. my other scripts are working fine and they take input only after i executed the script then added the variables. i wanted to execute the script and variables in one line. – Sami-M Apr 03 '16 at 14:03
2

In source, you need the path shell

#!/bin/sh
echo "my name is $1 I am $2 old."

after you set the permission, can you execute

$ chmod +x Test
$ ./Test $1 $2
1

One of the answers given so far is likely to be correct. But you didn't provide us with enough information to be sure (What O/S are you running? What -- if any -- response do you get when you run the script?), so here is how you can diagnose the problem yourself.

1) Enter the command

which Test

If the which command is not installed, skip ahead. If the answer is

/directory/containing/your/script/Test

then you know you are actually running your script. Other possibilities are

  • which: no Test in [list of all directories on your path] This means the Test command is simply not being found. I doubt this is the case, since you say it is in the same directory as other scripts that are working.

  • /usr/bin/test This means you are running cygwin under Windows, or some similar system where commands are not case sensitive. As a result, you are running the system test command instead of your Test script. Best bet: rename your script. (You could put your directory in the front of $PATH so that your script is found instead of the system command -- but that's likely to break something else that uses the system test command.)

2) In the absence of the which command, look at what response you get when you run

Test Joe 22
  • If there is no response at all, then odds are that you are running the system test command (see above for comments about cygwin, et al.)

  • If you get "Test: command not found" then either the script really isn't on your path, or you incorrectly entered the shebang (#!) line mentioned elsewhere. Note: having this line is good form, but in many environments it isn't necessary to get your script to run.

If you are still confused, send us

  • What operating system you are running under?
  • What response do you get when you run the script?

Optionally, but possibly helpful, add

  • Your current working directory (pwd)
  • The directory containing the Test script
  • The contents of your PATH variable (echo $PATH)
Phil Freed
  • 98
  • 7