LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-05-2001, 09:32 PM   #1
ChimpFace9000
Member
 
Registered: Jul 2001
Location: So. Cal.
Distribution: Slackware 8.1
Posts: 55

Rep: Reputation: 15
Post Alias


I understand that with the alias command i can do this...

alias d="ls"

Then type d, and it will execute that command. But can i use alias to set options for programs. Like if everytime im gonna use ls, im gonna use the -a option i can do this...

alias ls="ls -a"

But what if i want to add the -l option one time. What im trying to say is, is there a way to have a default set for a command, but also be able to change it on the command line? Let me know if i didnt explain that right.
 
Old 07-05-2001, 10:02 PM   #2
GonzoJohn
Member
 
Registered: Jun 2001
Location: Louisville, KY USA
Distribution: RedHat and Debian
Posts: 89

Rep: Reputation: 15
I wouldn't recommend using alias to change a command name, at least not the way you suggest. I'd recommend creating a simple script that runs the program you want with the variable you want and naming it something you'll remember, then put the script in your path.

Here's an example.

Let's use what you suggested, using "ls -a" as the command you want when you type "d". Simply create a text file with your favorite editor that contains the command "ls -a" and name it "d". Then chmod "d" to 755 so that the file is executable and move it to your /bin or /usr/bin directory. This way, "d" does what you want and "ls" still has the ability to use the many variables.

There is a much more complex way to use bash scripting to create a menu for the various "ls" variables, but it's been like 7 years since I did that sort of stuff, so this is simple, doesn't take much space, and it works.

Last edited by GonzoJohn; 07-05-2001 at 10:13 PM.
 
Old 07-05-2001, 10:12 PM   #3
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
You can create a shell function and add the function call to your alias command. Here is an example:

Add this function to your .kshrc file or whatever shell rc file you use.

Code:
newls()
{
  if [ $# -gt 0 ]; then
    ls $@
  else
    ls -a
  fi
}
Create your alias like this:

Code:
alias ls=newls
Hope this helps

Gary
 
Old 07-05-2001, 10:14 PM   #4
ChimpFace9000
Member
 
Registered: Jul 2001
Location: So. Cal.
Distribution: Slackware 8.1
Posts: 55

Original Poster
Rep: Reputation: 15
Not quite what im looking for. I didnt think i explained myself correctly, my fault. Let me try one more time. Lets use the elvis editor. Every time i use it i add an option that tells elvis to show the current line and colum at the bottom. So your way would be to put this in a script

elvis -option

-option isnt really the option, its just for example. Using that though, i couldnt specify a file on the command line. What i want is to be able to specify that each time i type "elvis somefile.ext" on the command line and press enter, it would automaticly add the option to it. Is that possible?
 
Old 07-05-2001, 10:22 PM   #5
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
When you use the alias command it will append any args to the tail end of the command typed. If you specify your alias like this:
Code:
alias vi='vi -i'
When you launch vi like:

vi filename

It will actually expand it like this:

vi -i filename
 
Old 07-05-2001, 10:45 PM   #6
ChimpFace9000
Member
 
Registered: Jul 2001
Location: So. Cal.
Distribution: Slackware 8.1
Posts: 55

Original Poster
Rep: Reputation: 15
Thanks everybody.
 
Old 01-13-2004, 03:25 AM   #7
iduru2
LQ Newbie
 
Registered: Jan 2004
Distribution: Fedora Core 1
Posts: 2

Rep: Reputation: 0
Hello,

I have been tying all night to figure out how to make a simple command alias just to get the idea of how to do it but to no avail. Here's what I've tried: In a text editor I entered "alias exit=bye" saved it made it executable and moved it to my bin directory. Now when I pull up a terminal window and enter bye nothing happens. Where am I going wrong here ?

Thanks for you help,
iduru2

Last edited by iduru2; 01-13-2004 at 03:29 AM.
 
Old 01-13-2004, 04:52 AM   #8
mikshaw
LQ Addict
 
Registered: Dec 2003
Location: Maine, USA
Distribution: Slackware/SuSE/DSL
Posts: 1,320

Rep: Reputation: 45
three things that I can see:

1) a shell script generally needs a shabang on the first line to specify it's a script (#!/bin/sh)
2) it seems you're declaring the alias backwards..."exit" is a standard command that you assign to an alias.
3) putting "alias bye=exit" in a script doesn't do anything except declare the alias. If you want to use alias, do as crabboy said....you can specify either a function as he wrote, or a command without a function. Then if you wanted to use that alias in a script, you just need to add the alias "bye".


As an example, in ${HOME}/.bashrc, add the line
alias bye=exit

Then you can type "bye" into a terminal and it will close. You could also add bye to a script and it will do the same thing.

Last edited by mikshaw; 01-13-2004 at 04:58 AM.
 
Old 01-14-2004, 12:56 AM   #9
iduru2
LQ Newbie
 
Registered: Jan 2004
Distribution: Fedora Core 1
Posts: 2

Rep: Reputation: 0
Please bear with me as I am very new to linux.
Where is 'bashrc' ?....in my bin directory ?
Once located where would I enter the 'alias bye=exit'

Thanks
iiduru2
 
Old 01-14-2004, 05:01 AM   #10
mikshaw
LQ Addict
 
Registered: Dec 2003
Location: Maine, USA
Distribution: Slackware/SuSE/DSL
Posts: 1,320

Rep: Reputation: 45
.bashrc probably wasn't automatically created.
It's a file placed in your home directory which contains bash configurations specific to your user.
As for where you enter it, it doesn't really matter as long as it is on its own line.
keep in mind that if you have any command options or multiple commands in your alias, it'll need to be quoted:
Code:
alias x='clear; exit'
alias mcd='mount /dev/cdrom'
and it's ".bashrc", with a dot.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Alias question (2 commands for one alias) gflores Linux - Newbie 3 01-21-2006 01:40 AM
ip alias mindcry Linux - Networking 0 10-16-2003 08:41 PM
using alias intruptz Linux - General 5 09-17-2003 05:32 AM
alias booyah Slackware 3 05-06-2003 04:17 PM
alias nobu Slackware 5 01-11-2003 02:09 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration