Why I obtain this error when I try to perform this simple bash script?

I am following this tutorial to correctly execute Aptana Studio on my Ubuntu 14.04:

  • Command menu in aptana studio 3 is not working in ubuntu 14.04

So I have installed the Oracle JRE and finnally I have create this run.sh file that have to execute Aptana:

!/bin/bash

export UBUNTU_MENUPROXY=0 /home/andrea/Programmi/Aptana_Studio_3/AptanaStudio3

The problem is that when I try to execute this script with my shell I obtain the following error message:

andrea@andrea-virtual-machine:~/Programmi/Aptana_Studio_3$ sudo sh run.sh
run.sh: 1: run.sh: !/bin/bash: not found
run.sh: 3: export: /home/andrea/Programmi/Aptana_Studio_3/AptanaStudio3: bad variable name

Why? What am I missing? How can I fix this issue?


There are two errors. In short, this is the particular fix I would suggest (details follow):

#!/bin/sh
UBUNTU_MENUPROXY=0 /home/andrea/Programmi/Aptana_Studio_3/AptanaStudio3

Bad Hashbang Line

As Shutupsquare says, your hashbang line (technical details) should start with a #! rather than just !.

That is causing the first of your two error messages, and will prevent your script from being run as ./run.sh, but is not the reason your script is failing to run AptanaStudio3 (because the hashbang line is not required for sh run.sh calling syntax).

By the way, when you run sh run.sh, it runs your script with sh as the interpreter. But with the hashbang line #!/bin/bash, running ./run.sh would run your script with bash as the interpreter. This script does not in any way depend on the advanced features of bash, and while bash's additional memory usage is almost certainly insignificant for this application, you may still wish to write the hashbang line so ./run.sh runs the script with sh:

#!/bin/sh

Incorrect Use of export

The main problem, which is causing your second error message and is the reason AptanaStudio3 fails to run, is that you're using export incorrectly. The export command does not run commands. So your command is being interpreted as an environment variable itself.

  • export can set and export an environment variable:

    export NAME=value
    
  • export can also export an environment variable (with whatever its current value is):

    export NAME
    

export does not accept NAME=value command or NAME command syntax, however.

Assuming your goal is to run the command /home/andrea/Programmi/Aptana_Studio_3/AptanaStudio3 with the UBUNTU_MENUPROXY variable defined and set to 0, you should just drop the word export altogether:

UBUNTU_MENUPROXY=0 /home/andrea/Programmi/Aptana_Studio_3/AptanaStudio3

That is the standard way to run a command with a modified environment, from a shell.

The purpose of export is to export variables into the environments of all subsequently launched child processes (i.e., every command run from your script). So running export UBUNTU_MENUPROXY=0 on its own line followed by the AptanaStudio3 command, as Shutupsquare suggests, will work, and have the same effect, and is a perfectly good and acceptable way to do it.

I prefer the way I've suggested above (without export) because:

  • Semantically, your goal is not to export anything, but rather to run a single command with a modified environment. Using export may cause confusion to others reading your script (or to yourself later), and as a secondary consideration is arguably less elegant.
  • With export, you need two commands; using the way without it, you need only one.

Optional reading: the env command works (sort of) the way you were trying to use export

Finally, there is a command that accepts NAME=value command syntax. This command is env. There's no reason to use it in this shell script--its function, in that basic usage, is to provide (part of) the power of a shell in running a command with a modified environment, and you already have that power.

However, under some circumstances, outside of shell scripting, you may find you need to set an environment variable for a command and run that command (and do those things in a single command). In contexts where just using the line VARIABLE=value command is not supported, you can use:

env VARIABLE=value command

You don't need that here though. All Bourne-style shells (e.g., sh, dash, bash, ksh, zsh) support VARIABLE=value command.


Should be #!/bin/bash. I also think that you should have a newline at the end of export UBUNTU_MENUPROXY=0.

#!/bin/bash

export UBUNTU_MENUPROXY=0 
/home/andrea/Programmi/Aptana_Studio_3/AptanaStudio3