Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash shell function error : command not found

Tags:

linux

bash

shell

I am new to bash so please bear with me if this is a dumb question :

What I actually want to type in the shell is like this:

javac -classpath "emarket.jar" Testclient.java -Xlint:unchecked

The fact is, if I manually type the above line into bash, it executes with no error. However, if I craft a customized function in .bashrc like this:

function compile() { 'javac -classpath "emarket.jar" '$@'.java -Xlint:unchecked';}

And issue this command in bash:

compile Testclient

It gets to an error saying that :

bash: javac -classpath "emarket.jar" Testclient.java -Xlint:unchecked: command not found

I reckon the function compile() in .bashrc should generate the same command in bash, but I really cannot get through this, can anyone help me? Many thanks in advance!

like image 712
Michael Mao Avatar asked Jun 24 '26 22:06

Michael Mao


2 Answers

remove the single quotes surrounding the whole command, and use double quotes around $@

function compile() { 
  javac -classpath "emarket.jar" "$@".java -Xlint:unchecked;
}

see here for quoting variables examples.

like image 158
ghostdog74 Avatar answered Jun 27 '26 23:06

ghostdog74


The problem is the ' quotes in your compile function. This causes the shell to not break things up at whitespace and treat the entire string as the command (rather than the first word as the command, and the rest as arguments.) Delete those and it should work

like image 42
Chris Dodd Avatar answered Jun 27 '26 23:06

Chris Dodd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!