2

Lets say I have a script which creates a file and writes something into it (but could be anything). It could be a script that runs a set of xrandr commands to set a resolution, for example.

Is there a way in which I can create a command (working only on my system) that executes the file, even if the file is not in my working directory?

TellMeWhy
  • 17,124
  • 39
  • 95
  • 141
  • Just speculation, but couldn't you add its parent directory to your $PATH? That would let you run it by typing just its name, wouldn't it? – TheWanderer Nov 20 '15 at 15:36
  • You can do something like [this](http://unix.stackexchange.com/a/84690) – Alex Lowe Nov 20 '15 at 15:44
  • 1
    For stuff that's specific to me, I put it in my ~/bin directory, and that directory is in my $PATH – glenn jackman Nov 20 '15 at 15:51
  • You could chuck it into a folder that appears in `echo $PATH` - or you can [add a folder to the the path](http://askubuntu.com/a/97898/178596) (note may not work with sudo). There must be atleast 50 questions already like this... – Wilf Nov 20 '15 at 16:04

1 Answers1

4

In Ubuntu, the default .profile adds your $HOME/bin directory to $PATH, assuming the former exists. This means that you can create a bin directory in your home, and any executables in there can be run without specifying the full path, which is what you want.

In the case of a Python script, also make sure that it's executable (chmod u+x blahblah) and it has the proper shebang on the first line, usually:

#!/usr/bin/python
Andrea Corbellini
  • 15,616
  • 2
  • 64
  • 81
roadmr
  • 33,892
  • 9
  • 80
  • 93
  • Additionally, after creating the `~/bin` directory, you need to either close and reopen your terminal window or run `source ~/.profile` again. Otherwise the PATH will not be updated yet. – Byte Commander Nov 20 '15 at 17:13