LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 08-30-2012, 08:46 AM   #1
iwambeke
LQ Newbie
 
Registered: Aug 2012
Posts: 4

Rep: Reputation: 0
Setting up alias in bash


Ok so in csh I have the following alias's and what they do
Code:
alias cl 'cd \!* && ls -alh'
alias g 'cl /proj/\!:1/rev\!:2/workarea/<user>/
The objective for cl is cd and list everything. g will go to a certain project and revision example g P09 45
goes to /proj/P09/rev45/workarea/<user>/. The g command is because I am constantly switching between projects and revisions.

Sometimes I need to use bash so I have tried setting up alias's in my .bashrc file exactly the same but as you know the \!* doesn't work. So far I have the following
Code:
alias cl="xargs -I {} bash -c 'cd {} && ls -alh' <<<"
This will print the contents in the specified location, but not stay in that location.

Any thoughts on how to write a file in bash to perform the same as in csh?

What I read bash says "bash has no mechanism for using argument in the replacement text, as in csh. If arguments are needed, a shell function should be used." I didn't really want to use a function though, I know someone out there can help me out, it is really close to being correct I think. Someone smart help me out here.

Last edited by iwambeke; 08-31-2012 at 04:46 PM. Reason: merge
 
Old 08-30-2012, 08:50 AM   #2
iwambeke
LQ Newbie
 
Registered: Aug 2012
Posts: 4

Original Poster
Rep: Reputation: 0
Sorry for the double post still new to linux and this forum don't know how to delete message

Last edited by iwambeke; 08-30-2012 at 11:22 AM.
 
Old 08-30-2012, 08:53 AM   #3
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
I'm not sure you could do it with an alias, but you could do it with a shell function

Just add to your .bashrc:

Code:
g(){
    cd $1
    ls -alh
}
EDIT: better not to double-post, just edit your last post. Also, what? Why don't you want to use a shell function? It works the same as an alias, essentially, but just is more powerful...
 
Old 08-30-2012, 12:46 PM   #4
iwambeke
LQ Newbie
 
Registered: Aug 2012
Posts: 4

Original Poster
Rep: Reputation: 0
Ok great I am really new to everything in Linux. I was trying to use an alias because it is really easy to do in csh. That function looks simple, and works great.

Now I am struggling with the second part, for csh first I have
Code:
alias g 'cl /home/\!:1/
alias g2 'cl /home/\!:1/rev\!:2/areas/<user>/
writing a function that can detect if 2 inputs or one are present so here is attempt but it doesn't work
Code:
cl(){
 cd $1
 ls -alh
}

g(){
   if["$2" = "??"] || ["$2" = "?"];then
     cl /home/"$1"/rev"$2"/areas/$USER/
   else
     cl /home/"$1"/
   fi
}
but the response
bash: /home/iwambeke/.bashrc: line 32: syntax error near unexpected token `then'
bash: /home/iwambeke/.bashrc: line 32: ` if["$2"="??"] || ["$2"="?"];then'

Last edited by iwambeke; 08-31-2012 at 04:45 PM.
 
Old 08-30-2012, 06:09 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,406

Rep: Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396
1. please use code tags when posting https://www.linuxquestions.org/quest...do=bbcode#code

2.
Code:
g()
{
if [[ $# -eq 2 ]]
then
    cl /home/"$1"/rev"$2"/areas/$USER/
else
    cl /home/"$1"/
fi
}
https://www.linuxquestions.org/quest...script-201221/
http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

PS Add
Code:
set -xv
into the top of a prog to see what the parser is doing (after the shebang line)
 
Old 08-30-2012, 06:22 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,826

Rep: Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973
First, please use ***[code][/code] tags*** around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, bolding, colors, or other fancy formatting.

Second, this...
Code:
alias cl="xargs -I {} bash -c 'cd {} && ls -alh' <<<"
...doesn't work because it spawns a new shell instance. The environment changes made there (i.e. pwd) are lost when that shell exits, so you end up back where you started.

Also, remember that an alias is just a text substitution feature, not a command. If the first token (word) on the line matches a preset alias, the text of the alias is inserted at that point. Everything after the first token stays where it is. So there's no way to position arbitrary arguments that way. That's what makes shell functions so much more flexible. They're basically scripts that execute inside the current environment.

As for your second one, first, understand that "[" is a command (an synonym for test), and everything after it is an argument to that command, so they need to be whitespace separated. Also, test doesn't have pattern-matching ability.

As long as you're using bash, you should use the new "[[" keyword instead, which is broadly compatible, but more flexible, and does support pattern matching.

Speaking of which, I'm not really sure what you are doing here:
Code:
[ "$2" = "??" ]
This appears to be trying to match a single, two letter string. Is that right? But not only is it the old test, it's also quoted, which disables the globbing power of "?". Or perhaps this is some attempt to carry over syntax from csh.

For pattern matching two characters (of any kind) in bash, you'd use this:
Code:
[[ $2 == ?? ]]
But I'm not sure this is what you really want anyway. I think you just want to know if a $2 entry exists. So just use this:

Code:
if [[ $2 ]]; then
I suggest also putting in some sanity code too, such as a test to see if the directory exists before changing to it.

See the following links for more on bash tests. When using bash or ksh, it's recommended to use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.

http://mywiki.wooledge.org/ArithmeticExpression
http://mywiki.wooledge.org/BashFAQ/031
http://wiki.bash-hackers.org/commands/classictest
http://wiki.bash-hackers.org/syntax/...nal_expression
http://wiki.bash-hackers.org/syntax/arith_expr
 
1 members found this post helpful.
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
setting alias in bash, permanently allelopath Linux - General 6 10-24-2008 04:10 AM
Setting an Alias stoyboy Programming 2 06-13-2008 03:31 AM
setting up scp alias under bash - parameter passing problem gjr Linux - Newbie 4 06-08-2005 06:38 AM
setting up an alias in apache tarballed Linux - Networking 3 01-31-2003 04:11 PM
ip alias and router setting thanhnx Linux - Networking 20 01-26-2003 07:45 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:14 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