Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying to run basic functions on Perl (Mac) but I get the same error message all the time. I managed to run programs like the simple 'hello, world' example. They work fine:

$ perl -w hello.pl
Hello World

But when I try different things, such as assigning a value to a variable, it gives me a message such as:

$number = 13*2;
-bash: =: command not found

Commands such as list seem to work, but I get the bash error all the time. I read that it can be related to the path but not sure how to exactly verify this and what's the bash doing here.

share|improve this question
1  
Should we understand that you entered number = 13*2; to the bash prompt? If yes, remove the spaces around the = sign. –  manatwork Sep 26 '12 at 12:24
    
I added it to the new-host:~ prompt I get in the shell. The error I get it on the bash: -bash: =13*2: command not found –  jose Sep 26 '12 at 12:28
    
Just a moment. In you second quoted command the $ symbolizes the bash prompt or you typed it literally? If the later, then bash expanded it to nothing then tried to execute the resulted command: =13*2. You can not just mix bash and perl syntax like that. –  manatwork Sep 26 '12 at 12:37
    
new-host:~ jose$ perl -w hello.pl Hello World new-host:~ jose$ #this is what i obtain running the 'hello.pl' program. I'm only trying to use perl; not sure I understand why is it mixing it with bash (not intended at all). I understood that the $ sign is used for assigning values to variables in perl. The bash prompt appears with the error. –  jose Sep 26 '12 at 12:42
    
These are my permissions: crw--w---- #could this be part of the problem? –  jose Sep 26 '12 at 12:48
add comment

2 Answers

You seem to be confusing bash with perl. The default shell on your mac is bash, which cannot set variables in the same way, or using the same syntax, as perl. To set a variable called '$number' to the result of '13 * 2' using perl, on a bash CLI, you would:

my-macbook:~ $ perl -e '$number = 13 * 2; print $number, "\n";'
26

To do the same thing in bash itself, you could:

my-macbook:~ $ number=$(expr 13 \* 2); echo $number
26

You can't put raw perl syntax into your bash shell in Terminal, because the bash shell running in Terminal is expecting bash syntax. Bash is a whole language unto itself, and is not compatible with perl. To tell bash that you want to execute some code using perl instead of bash, you use Perl's -e flag (as in e for execute), then wrap your perl code in single quotes.

share|improve this answer
1  
Thanks Tim, I finally understood the way it works and what I was doing wrong; now I moved on significantly. –  jose Sep 26 '12 at 17:57
2  
Hi Jose -- if this response helped you resolve your question, don't forget to approve the answer (with the checkmark under the voting arrows) –  Jim Paris Sep 26 '12 at 19:53
add comment

To do basic arithmetic in bash (the shell running inside your terminal emulator) you would use one of the following constructs. The $ prefix is used with variables in bash when getting the value of the variables, not in their assignment:

my_product=$(( 13 * 2 ))
my_product=$[ 13 * 2 ]
echo $my_product

As already stated perl is not bash so you have to make sure to use the right syntax with the right language.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.