You are not logged in.

#1 2012-05-07 17:56:18

Wasser
Member
Registered: 2011-07-06
Posts: 23

[SOLVED] Using arguments in Bash Alias

Hi all!

I'm not very well-versed in the  art of bash scripting or anything and I am having some trouble doing an alias that uses two arguments.

My case:

My command:
"openssl aes-256-cbc -a -salt -in a.txt -out a.txt.aes"

What I wanted to do was an alias like this:
alias encrypt="openssl aes-256-cbc -a -salt -in $1 -out $2"

But it's not working. Actually, even using just the $1 and leaving a fixed string as $2 is not working.

Can anyone give me some help? Thanks!

Last edited by Wasser (2012-05-09 22:45:56)

Offline

#2 2012-05-07 18:02:54

Unia
Member
From: The Netherlands
Registered: 2010-03-30
Posts: 2,471
Website

Re: [SOLVED] Using arguments in Bash Alias

You gotta use single quote's (not sure of the correct English term):

' instead of "

Also, remember to reload your .bashrc after changing it.

Last edited by Unia (2012-05-07 18:03:10)


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#3 2012-05-07 18:02:59

xduugu
Member
Registered: 2008-10-16
Posts: 289

Re: [SOLVED] Using arguments in Bash Alias

Afaik, that's not possible with bash's aliases. But you can define a function:

encrypt() { openssl aes-256-cbc -a -salt -in "$1" -out "$2"; }

Offline

#4 2012-05-07 18:03:39

WorMzy
Forum Moderator
From: Scotland
Registered: 2010-06-16
Posts: 5,265

Re: [SOLVED] Using arguments in Bash Alias

Use a function, or make a script and stick in your path.


Sakura:-
Mobo: ASUS P8Z77-V PRO // Processor: Intel Core i7-3770K 3.4GHz // GFX: nVidia GeForce GTX 970 Ti // RAM: 32GB (4x 8GB) Corsair DDR3 (@ 2133MHz) // Storage: 1x 3TB Seagate SATAII 5x 1TB Samsung SATAII, 2x 120GB Corsair SSD

Offline

#5 2012-05-07 18:09:52

Wasser
Member
Registered: 2011-07-06
Posts: 23

Re: [SOLVED] Using arguments in Bash Alias

Thanks to all.

I will use a function (the single quote does not work).

But why does this happen?

Offline

#6 2012-05-07 18:48:32

karol
Archivist
Registered: 2009-05-06
Posts: 25,428

Re: [SOLVED] Using arguments in Bash Alias

Wasser wrote:

But why does this happen?

Aliases simply don't take arguments, functions do (just like the mathematical functions).

Offline

#7 2012-05-07 18:53:21

Wasser
Member
Registered: 2011-07-06
Posts: 23

Re: [SOLVED] Using arguments in Bash Alias

karol wrote:
Wasser wrote:

But why does this happen?

Aliases simply don't take arguments, functions do (just like the mathematical functions).

Thanks big_smile

Offline

#8 2012-05-07 19:47:37

jjacky
Member
Registered: 2011-11-09
Posts: 321
Website

Re: [SOLVED] Using arguments in Bash Alias

karol wrote:

Aliases simply don't take arguments, functions do (just like the mathematical functions).

Well, though one can of course use arguments with aliases, e.g.

alias p='sudo pacman'

still allows you do specify options, etc of course, such as:

p -Syu foobar

Might be "better"/less confusing to say that an alias is just a simple string replacement, and for more "complicated" things one needs indeed to e.g. use a function.

Offline

#9 2012-05-07 20:24:15

karol
Archivist
Registered: 2009-05-06
Posts: 25,428

Re: [SOLVED] Using arguments in Bash Alias

Sure, I often use aliases that way. as

alias foo="bar baz"

and

foo () { bar baz $1; }

are equivalent:

$ foo <some argument>

Offline

#10 2012-05-08 08:52:42

Wasser
Member
Registered: 2011-07-06
Posts: 23

Re: [SOLVED] Using arguments in Bash Alias

Ok. So actually the problem is replacing arguments in the middle of the command.

Offline

#11 2012-05-08 09:21:58

/dev/zero
Member
From: Melbourne, Australia
Registered: 2011-10-20
Posts: 1,176
Website

Re: [SOLVED] Using arguments in Bash Alias

Wasser wrote:

Ok. So actually the problem is replacing arguments in the middle of the command.

Correct! You might also be interested in this recent thread, from about 6 posts down onwards: https://bbs.archlinux.org/viewtopic.php?id=140146

Please remember to mark the thread solved smile.


Linux is NOT Windows | The Rootless Root
Toshiba Satellite i5-3230M 2.6GHz CPUs, 4Gb RAM, ArchLinux, wmii, nVidia GeForce GT 740M.

Offline

Board footer

Powered by FluxBB