• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 319
  • Last Modified:

Linux: Run custom script without path

From the command line, I want "zzz" to echo 'Hello World'.

I created a file named 'zzz' and changed the permissions to 755:
#!/bin/sh
echo Hello World

Open in new window


It works when I type this in from a command line:
./zzz

Open in new window


How can I get it to work without the dot and slash at the beginning?


I use CentOS 6.

0
hankknight
Asked:
hankknight
  • 4
  • 4
  • 3
  • +3
10 Solutions
 
themrrobertCommented:
copy the file to /usr/bin
0
 
edster9999Commented:
or if you do not have access to the /usr/bin
then leave it in your own directory and set up an alias for it with something like :

alias zzz="/home/hankknight/zzz"

0
 
hankknightAuthor Commented:
Thanks, I don't have root access.  Using "alias" works.  How can I remove the alias later?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
sentnerCommented:
Another option is to put it into any other directory that is in your PATH.  I always create a "bin" directory in my home directory, and add ~/bin to my PATH (edit your .login, .profile, etc, to set the PATH for your shell login). Do not add ~ to the path directly though, as that can cause a security risk.
0
 
sentnerCommented:
As for how to remove an alias, it is only persistent during the current shell session unless you've added it to your startup files such as .profile.  If you exit the shell, the alias will be gone.  Depending on the shell you can also remove it with the unalias command.  
0
 
edster9999Commented:
unalias zzz
0
 
Kerem ERSOYPresidentCommented:
hi,

Edit your path to include the current directory. such as:

export PATH=.:$PATH


then you can execute the command in current directory without leading ./

such as:

zzz

Cheers,
K.
0
 
Kerem ERSOYPresidentCommented:
If you want you can edit your .bash_profile to include the path command so that it will be available every-time you logon.

After the modification execute this command:

source .bash_profile

so that it will reload your profile script.

Cheers,
K.
0
 
Kerem ERSOYPresidentCommented:
none of these methods require root access !!!!
0
 
TintinCommented:
Personally, i wouldn't bother writing a script for something so simple.  Just use an alias


alias zzz='echo Hello World'

Open in new window

0
 
sentnerCommented:
You should NEVER add "." to the path, especially at the front, as that can lead to a security vulnerability.  It would be simple for anyone to place a script into a location that you might go to (such as /tmp), give it a name such as "ls", and if you changed to that directory and run "ls", you would run that script with your user's permissions.  
0
 
Kerem ERSOYPresidentCommented:
> You should NEVER add "." to the path, especially at the front, as that can lead to a security vulnerability.

To address this he'd better use this :

export PATH=$PATH:.





0
 
sentnerCommented:
Even that isn't safe. Only put hard (not relative) paths in the PATH variable.  It's ok to do PATH=$PATH:~/bin but not PATH=$PATH:.
0
 
edster9999Commented:
Agree with sentner. Nover add '.' to the path.  This will allow you to run commands in the current location but it very lazy and does lead to security issues and gets you stuck in loops that cause issues.  Very bad idea.

The options are to add a safe path (as sentner said) like ~/bin which would allow you to run the command by placing it into a bin directory under your home directory, or to use an alias as per my example.

I guess it comes down to how many you want to use.  If you are going to add lots of command then use ~/bin in your path.  If you wish to do a single command or two then use alias.

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 4
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now