Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
As of course you can see i am new to all this. But my knowledge is growing as i learn more and more . My issue here is, i want to be able to make a command. For example i want to say make a command that does the same as clear but instead of typing clear, i can just type cls. Another example is run more than one command after another. Say rn the uptime command and than the netstat command. Can someone show me how to do this? thanks
The first is a simple alias. It gives one command a new name so you can type the alias to do the same thing. Under bash:
alias cls=clear
Then you can just type 'cls' and it will run clear.
This only lets you alias a single command though (afaik), for more complicated shortcuts you will have to create a script file.
Open a new text file and enter the following:
#!/bin/bash
# This is a comment line
clear
The first line tells linux to use the bash interpreter to read this file.
Save the file, named 'cls'
You now have to change the permissions so that it is executable:
chmod u+x cls
now you can type 'cls' and will execute that file. If you want to be able to execute it from anywhere, you have to copy it to somewhere that appears in your PATH variable.
Thats about it..... of course the script files can be as big as you like, and can take arguments and options just like all the regular command line programs. Script files are used in the linux system for all kinds of goodness, including the boot sequence etc etc.
thanks for the help guys, but i have a problem. I did as you said and within that directory i type in that command and no go. it just says cls: command not found. Any ideas?
If you mean you wrote the file, try './cls' if the current directory isn't on the path, either as 'current directory' or the full path. (And this assumes you remembered to make it executable.) If you created the alias from the prompt, I dunno what's wrong, unless you tried it in another xterm or terminal. To make the alias take effect generally, it needs to be in the bash configs. My ~/.bash_profile has
alias c='clear'
and ~/.bashrc is a symlink to it.
-- Oh, you can always do ctlrl-l in character mode and usually in a terminal emulator if that'd be easier.
instead, then it should work, if the executable permissions have been set. (Use ls -l to see the permissions.)
If this works, then the problem was that your PATH variable does not include the current directory, so it could not find the file.
Do:
echo $PATH
to see the list of directories that will be searched, seperated by semicolons. You could try adding the current directory to the path variable:
export PATH=$PATH:.
and then try cls again (without the ./ in front) and it should find it.
Yeah - and not that it's that big of a deal but you might choose not to add the current directory to your path (marginal security issue) but create a directory to hold your scripts and put that on the path. I do that just so I can find them more easily if I need to edit them and keep up with what's what and so on.
Thanks for the help, i am guessing any scrips made in a directory other than the ones being searched have to have ./name in order for it to work, but as well as be in the directory of that script? I just made the script and than copied that script to the /usr/bin directory. The command you gave about the export PATH=$PATH, does that alow me to add a directory with all my scripts in there to be searched?
Well, . (a period by itself) to the shell is a
reference to the current directory ... so, if
you want to call the script using ./ you need
to be in the same directory ... you can, however,
reference it with ~/<name> if it lives in your
home, and it should work from where ever you
called it ...
.., btw, is a parent directory ... so if you were
in /usr/local/bin, and the script your're calling
were in /usr/bin you could do ../../bin/<name> :}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.