LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-20-2020, 06:57 PM   #1
dave555
LQ Newbie
 
Registered: Dec 2019
Posts: 9

Rep: Reputation: Disabled
How can I use a parameter with an alias?


Hello

I need a little help with this. I am making an alias to create a password quickly.

This works:

Code:
alias makepw="cat /dev/urandom | tr -cd '[:alnum:]%$#-' | head -c 15 ; echo"
~ % makepw 
9oFpSu4N%5VDIYF
However, when I change to head -c $1, I get this error
Code:
alias makepw="cat /dev/urandom | tr -cd '[:alnum:]%$#-' | head -c $1 ; echo"
~ % makepw 19
head: option requires an argument -- 'c'
Try 'head --help' for more information.
19
I'm trying to use $1 as an argument to pass different lengths for a password.

PS: The command without the alias part works fine if used in it's own shell script file, BUT I want to use it as an alias in my .bashrc file.

Any ideas what I am doing wrong? Thanks in advance

Last edited by dave555; 01-20-2020 at 07:01 PM.
 
Old 01-20-2020, 07:06 PM   #2
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.7.1908
Posts: 4,373

Rep: Reputation: 1539Reputation: 1539Reputation: 1539Reputation: 1539Reputation: 1539Reputation: 1539Reputation: 1539Reputation: 1539Reputation: 1539Reputation: 1539Reputation: 1539
Try aliasing the script that contains the command.
 
Old 01-20-2020, 07:10 PM   #3
Geist
Member
 
Registered: Jul 2013
Distribution: Slackware 14 / current
Posts: 367

Rep: Reputation: Disabled
If you want something fancy like this, since alias really is just supposed to be an alias and not a 'script', it's better to make a function with the same name, or a script.

Edit: You can put this in .bashrc, after all.
Code:
function makepw(){
 cat /dev/urandom | tr -cd '[:alnum:]%$#-' | head -c $1 ; echo 
}

Code:
makepw 144                                                                         
6Hwg0Eab9MDHfKzqzXcd1kPdKJrfLq3slrlj-5wdr7xDMj#tdwNPerJ0-69$E-crdtdxXpc7Dk4wq7M0w%znCB-dCNwS9cR3A-TZSavC7s00xY0ipXuzD0xj$UTjpOABtC2iykDWqw8QXc#L
This can go further.
If you name a function after a command then the function, I think, should take precedence.
I use this approach to compile a suite of emulators that check which architecture the OS is in a not too robust way, and my computers hostname happened to trigger that check in a way that it fails.
So, whenever I need to compile it, I define the command as a function that then calls the actual command with the appropriate parameters and acts like the stand in for that command.
Note, if you want to do that at some point, call the actual command with "command actual_command"

So, for ls, for example:
Code:
function ls() { 
command ls -la 
}

Last edited by Geist; 01-20-2020 at 07:19 PM.
 
5 members found this post helpful.
Old 01-20-2020, 07:31 PM   #4
dave555
LQ Newbie
 
Registered: Dec 2019
Posts: 9

Original Poster
Rep: Reputation: Disabled
WOW! Thanks Geist. It worked like a charm.

I was researching and read the bash alias can't use arguments like $1,$2, $3, etc. You need to create a function instead.

Thanks again
 
Old 01-20-2020, 07:37 PM   #5
Geist
Member
 
Registered: Jul 2013
Distribution: Slackware 14 / current
Posts: 367

Rep: Reputation: Disabled
Yeah, functions are a lot more flexible.

There might be a catch, though, when it comes to other things using your functions, like trying to use it in a script.

You might have to add
Code:
export -f makepw
To your .bashrc to make the function accessible to things like that, but if you call this function in your shell on your own, then that's fine.
If trying to call it from elsewhere fails, then export it.

Last edited by Geist; 01-20-2020 at 07:39 PM.
 
1 members found this post helpful.
Old 01-20-2020, 08:23 PM   #6
dave555
LQ Newbie
 
Registered: Dec 2019
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Geist View Post
Yeah, functions are a lot more flexible.

There might be a catch, though, when it comes to other things using your functions, like trying to use it in a script.

You might have to add
Code:
export -f makepw
To your .bashrc to make the function accessible to things like that, but if you call this function in your shell on your own, then that's fine.
If trying to call it from elsewhere fails, then export it.
Thanks Geist for that additional info. Now I know. Thanks again
 
  


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
Can an alias defined in bashrc be called by another alias in same file? Usalabs Linux - General 7 08-28-2019 06:05 PM
How is parameter positioned in alias ? BudiKusasi Linux - Software 2 02-11-2018 12:19 PM
shell getopts: opt w/ optional parameter is taking next opt as its parameter! GrapefruiTgirl Programming 22 10-27-2010 07:00 AM
linux bash - how to use a dynamic parameter in shell parameter expansion expression nickleus Linux - General 2 08-21-2006 05:54 AM
setting up scp alias under bash - parameter passing problem gjr Linux - Newbie 4 06-08-2005 07:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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