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: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
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: 292

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: 11,788
Website

Re: [SOLVED] Using arguments in Bash Alias

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


Sakura:-
Mobo: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD

Making lemonade from lemons since 2015.

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,440

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: 347
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,440

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,247

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.

Offline

Board footer

Powered by FluxBB