Difference when executing bash function in an alias
up vote
1
down vote
favorite
I have a function in my .bash_profile
for printing text some pre-written text and copying it to the clipboard.
copyandprint ()
s='\033[1;32m' #strong
n='\033[0m' #normal
printf -- "printf -- '$1'
I use this to alias things I keep wanting to paste into other applications, like static IDs, or just silly things that are difficult to type quickly on a keyboard.
alias shrug=$( copyandprint '¯_(ツ)_/¯')
But when I wanted to use it with text generated at the time I use the alias, I can't just call it in the alias definition; the alias needs to call it.
alias copydate=$( copyandprint "$(date)" )
The value is generated when the script is run, not when the alias is used.
Through pretty much sheer trial and error, I was able to make a modified version of the function that does what I wanted:
copyandprint_live ()
s='\033[1;32m' #strong
n='\033[0m' #normal
printf -- "$1"
alias copydate_live="$( copyandprint_live "$(date)" )"
The date is generated at the time the alias is used, rather than at the time the script is executed.
But when I use that function the way I used the other one, it fails:
alias shrug_2=$( copyandprint_live '¯_(ツ)_/¯')
$ shrug_2
#=> -bash: syntax error near unexpected token `ツ'
And I tried putting double quotes, but that didn't work
alias shrug_3=$( copyandprint_live '"¯_(ツ)_/¯"')
$ shrug_3
#=> copied 033[1
#=> -bash: 32m¯_(ツ)_/¯033[0m: No such file or directory
My question is, what's going on here? Why do they need to be so different?
bash alias
add a comment |
up vote
1
down vote
favorite
I have a function in my .bash_profile
for printing text some pre-written text and copying it to the clipboard.
copyandprint ()
s='\033[1;32m' #strong
n='\033[0m' #normal
printf -- "printf -- '$1'
I use this to alias things I keep wanting to paste into other applications, like static IDs, or just silly things that are difficult to type quickly on a keyboard.
alias shrug=$( copyandprint '¯_(ツ)_/¯')
But when I wanted to use it with text generated at the time I use the alias, I can't just call it in the alias definition; the alias needs to call it.
alias copydate=$( copyandprint "$(date)" )
The value is generated when the script is run, not when the alias is used.
Through pretty much sheer trial and error, I was able to make a modified version of the function that does what I wanted:
copyandprint_live ()
s='\033[1;32m' #strong
n='\033[0m' #normal
printf -- "$1"
alias copydate_live="$( copyandprint_live "$(date)" )"
The date is generated at the time the alias is used, rather than at the time the script is executed.
But when I use that function the way I used the other one, it fails:
alias shrug_2=$( copyandprint_live '¯_(ツ)_/¯')
$ shrug_2
#=> -bash: syntax error near unexpected token `ツ'
And I tried putting double quotes, but that didn't work
alias shrug_3=$( copyandprint_live '"¯_(ツ)_/¯"')
$ shrug_3
#=> copied 033[1
#=> -bash: 32m¯_(ツ)_/¯033[0m: No such file or directory
My question is, what's going on here? Why do they need to be so different?
bash alias
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a function in my .bash_profile
for printing text some pre-written text and copying it to the clipboard.
copyandprint ()
s='\033[1;32m' #strong
n='\033[0m' #normal
printf -- "printf -- '$1'
I use this to alias things I keep wanting to paste into other applications, like static IDs, or just silly things that are difficult to type quickly on a keyboard.
alias shrug=$( copyandprint '¯_(ツ)_/¯')
But when I wanted to use it with text generated at the time I use the alias, I can't just call it in the alias definition; the alias needs to call it.
alias copydate=$( copyandprint "$(date)" )
The value is generated when the script is run, not when the alias is used.
Through pretty much sheer trial and error, I was able to make a modified version of the function that does what I wanted:
copyandprint_live ()
s='\033[1;32m' #strong
n='\033[0m' #normal
printf -- "$1"
alias copydate_live="$( copyandprint_live "$(date)" )"
The date is generated at the time the alias is used, rather than at the time the script is executed.
But when I use that function the way I used the other one, it fails:
alias shrug_2=$( copyandprint_live '¯_(ツ)_/¯')
$ shrug_2
#=> -bash: syntax error near unexpected token `ツ'
And I tried putting double quotes, but that didn't work
alias shrug_3=$( copyandprint_live '"¯_(ツ)_/¯"')
$ shrug_3
#=> copied 033[1
#=> -bash: 32m¯_(ツ)_/¯033[0m: No such file or directory
My question is, what's going on here? Why do they need to be so different?
bash alias
I have a function in my .bash_profile
for printing text some pre-written text and copying it to the clipboard.
copyandprint ()
s='\033[1;32m' #strong
n='\033[0m' #normal
printf -- "printf -- '$1'
I use this to alias things I keep wanting to paste into other applications, like static IDs, or just silly things that are difficult to type quickly on a keyboard.
alias shrug=$( copyandprint '¯_(ツ)_/¯')
But when I wanted to use it with text generated at the time I use the alias, I can't just call it in the alias definition; the alias needs to call it.
alias copydate=$( copyandprint "$(date)" )
The value is generated when the script is run, not when the alias is used.
Through pretty much sheer trial and error, I was able to make a modified version of the function that does what I wanted:
copyandprint_live ()
s='\033[1;32m' #strong
n='\033[0m' #normal
printf -- "$1"
alias copydate_live="$( copyandprint_live "$(date)" )"
The date is generated at the time the alias is used, rather than at the time the script is executed.
But when I use that function the way I used the other one, it fails:
alias shrug_2=$( copyandprint_live '¯_(ツ)_/¯')
$ shrug_2
#=> -bash: syntax error near unexpected token `ツ'
And I tried putting double quotes, but that didn't work
alias shrug_3=$( copyandprint_live '"¯_(ツ)_/¯"')
$ shrug_3
#=> copied 033[1
#=> -bash: 32m¯_(ツ)_/¯033[0m: No such file or directory
My question is, what's going on here? Why do they need to be so different?
bash alias
bash alias
edited Nov 9 at 20:36
asked Nov 9 at 20:31
Nathan Hinchey
675418
675418
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
Dispensing with the aliases and using functions makes this a lot easier.
copyandprint () pbcopy
printf 'copied 33[1;32m%s33[0m to clipboardn' "$1"
shrug ()
copyandprint '¯_(ツ)_/¯'
copydate ()
copyandprint "$(date)"
Functions work alike any other command:
$ foo () echo hi;
$ foo
hi
1
If you are referring to myshrug
function, there is zero benefit to defining the aliasshruggie
like that; you're just forking an extra shell to run something that can run in your current shell.
– chepner
Nov 9 at 21:05
The comment chepner is replying to above said: "You can also define an alias using that:alias shruggie="$(shrug)"
"
– Nathan Hinchey
Nov 9 at 21:18
But how do I use it in the terminal without defining it as an alias? The whole point of this was to have terminal aliases
– Nathan Hinchey
Nov 9 at 21:19
1
A function is just a named command. You don't need an alias to refer to it; you just use it.
– chepner
Nov 9 at 21:20
1
I redefinedcopyandprint
to actually do the copying and printing, rather than generating the body of an alias.
– chepner
Nov 9 at 21:24
|
show 1 more comment
up vote
0
down vote
You're calling the function when you define the aliases, not when you use them. You need to put the alias definition in single quotes to prevent $(...)
from executing the command at that time.
alias shrug='$( copyandprint "¯_(ツ)_/¯")'
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Dispensing with the aliases and using functions makes this a lot easier.
copyandprint () pbcopy
printf 'copied 33[1;32m%s33[0m to clipboardn' "$1"
shrug ()
copyandprint '¯_(ツ)_/¯'
copydate ()
copyandprint "$(date)"
Functions work alike any other command:
$ foo () echo hi;
$ foo
hi
1
If you are referring to myshrug
function, there is zero benefit to defining the aliasshruggie
like that; you're just forking an extra shell to run something that can run in your current shell.
– chepner
Nov 9 at 21:05
The comment chepner is replying to above said: "You can also define an alias using that:alias shruggie="$(shrug)"
"
– Nathan Hinchey
Nov 9 at 21:18
But how do I use it in the terminal without defining it as an alias? The whole point of this was to have terminal aliases
– Nathan Hinchey
Nov 9 at 21:19
1
A function is just a named command. You don't need an alias to refer to it; you just use it.
– chepner
Nov 9 at 21:20
1
I redefinedcopyandprint
to actually do the copying and printing, rather than generating the body of an alias.
– chepner
Nov 9 at 21:24
|
show 1 more comment
up vote
2
down vote
accepted
Dispensing with the aliases and using functions makes this a lot easier.
copyandprint () pbcopy
printf 'copied 33[1;32m%s33[0m to clipboardn' "$1"
shrug ()
copyandprint '¯_(ツ)_/¯'
copydate ()
copyandprint "$(date)"
Functions work alike any other command:
$ foo () echo hi;
$ foo
hi
1
If you are referring to myshrug
function, there is zero benefit to defining the aliasshruggie
like that; you're just forking an extra shell to run something that can run in your current shell.
– chepner
Nov 9 at 21:05
The comment chepner is replying to above said: "You can also define an alias using that:alias shruggie="$(shrug)"
"
– Nathan Hinchey
Nov 9 at 21:18
But how do I use it in the terminal without defining it as an alias? The whole point of this was to have terminal aliases
– Nathan Hinchey
Nov 9 at 21:19
1
A function is just a named command. You don't need an alias to refer to it; you just use it.
– chepner
Nov 9 at 21:20
1
I redefinedcopyandprint
to actually do the copying and printing, rather than generating the body of an alias.
– chepner
Nov 9 at 21:24
|
show 1 more comment
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Dispensing with the aliases and using functions makes this a lot easier.
copyandprint () pbcopy
printf 'copied 33[1;32m%s33[0m to clipboardn' "$1"
shrug ()
copyandprint '¯_(ツ)_/¯'
copydate ()
copyandprint "$(date)"
Functions work alike any other command:
$ foo () echo hi;
$ foo
hi
Dispensing with the aliases and using functions makes this a lot easier.
copyandprint () pbcopy
printf 'copied 33[1;32m%s33[0m to clipboardn' "$1"
shrug ()
copyandprint '¯_(ツ)_/¯'
copydate ()
copyandprint "$(date)"
Functions work alike any other command:
$ foo () echo hi;
$ foo
hi
edited Nov 9 at 21:20
answered Nov 9 at 20:39
chepner
239k29225319
239k29225319
1
If you are referring to myshrug
function, there is zero benefit to defining the aliasshruggie
like that; you're just forking an extra shell to run something that can run in your current shell.
– chepner
Nov 9 at 21:05
The comment chepner is replying to above said: "You can also define an alias using that:alias shruggie="$(shrug)"
"
– Nathan Hinchey
Nov 9 at 21:18
But how do I use it in the terminal without defining it as an alias? The whole point of this was to have terminal aliases
– Nathan Hinchey
Nov 9 at 21:19
1
A function is just a named command. You don't need an alias to refer to it; you just use it.
– chepner
Nov 9 at 21:20
1
I redefinedcopyandprint
to actually do the copying and printing, rather than generating the body of an alias.
– chepner
Nov 9 at 21:24
|
show 1 more comment
1
If you are referring to myshrug
function, there is zero benefit to defining the aliasshruggie
like that; you're just forking an extra shell to run something that can run in your current shell.
– chepner
Nov 9 at 21:05
The comment chepner is replying to above said: "You can also define an alias using that:alias shruggie="$(shrug)"
"
– Nathan Hinchey
Nov 9 at 21:18
But how do I use it in the terminal without defining it as an alias? The whole point of this was to have terminal aliases
– Nathan Hinchey
Nov 9 at 21:19
1
A function is just a named command. You don't need an alias to refer to it; you just use it.
– chepner
Nov 9 at 21:20
1
I redefinedcopyandprint
to actually do the copying and printing, rather than generating the body of an alias.
– chepner
Nov 9 at 21:24
1
1
If you are referring to my
shrug
function, there is zero benefit to defining the alias shruggie
like that; you're just forking an extra shell to run something that can run in your current shell.– chepner
Nov 9 at 21:05
If you are referring to my
shrug
function, there is zero benefit to defining the alias shruggie
like that; you're just forking an extra shell to run something that can run in your current shell.– chepner
Nov 9 at 21:05
The comment chepner is replying to above said: "You can also define an alias using that:
alias shruggie="$(shrug)"
"– Nathan Hinchey
Nov 9 at 21:18
The comment chepner is replying to above said: "You can also define an alias using that:
alias shruggie="$(shrug)"
"– Nathan Hinchey
Nov 9 at 21:18
But how do I use it in the terminal without defining it as an alias? The whole point of this was to have terminal aliases
– Nathan Hinchey
Nov 9 at 21:19
But how do I use it in the terminal without defining it as an alias? The whole point of this was to have terminal aliases
– Nathan Hinchey
Nov 9 at 21:19
1
1
A function is just a named command. You don't need an alias to refer to it; you just use it.
– chepner
Nov 9 at 21:20
A function is just a named command. You don't need an alias to refer to it; you just use it.
– chepner
Nov 9 at 21:20
1
1
I redefined
copyandprint
to actually do the copying and printing, rather than generating the body of an alias.– chepner
Nov 9 at 21:24
I redefined
copyandprint
to actually do the copying and printing, rather than generating the body of an alias.– chepner
Nov 9 at 21:24
|
show 1 more comment
up vote
0
down vote
You're calling the function when you define the aliases, not when you use them. You need to put the alias definition in single quotes to prevent $(...)
from executing the command at that time.
alias shrug='$( copyandprint "¯_(ツ)_/¯")'
add a comment |
up vote
0
down vote
You're calling the function when you define the aliases, not when you use them. You need to put the alias definition in single quotes to prevent $(...)
from executing the command at that time.
alias shrug='$( copyandprint "¯_(ツ)_/¯")'
add a comment |
up vote
0
down vote
up vote
0
down vote
You're calling the function when you define the aliases, not when you use them. You need to put the alias definition in single quotes to prevent $(...)
from executing the command at that time.
alias shrug='$( copyandprint "¯_(ツ)_/¯")'
You're calling the function when you define the aliases, not when you use them. You need to put the alias definition in single quotes to prevent $(...)
from executing the command at that time.
alias shrug='$( copyandprint "¯_(ツ)_/¯")'
answered Nov 9 at 20:42
Barmar
413k34238339
413k34238339
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232877%2fdifference-when-executing-bash-function-in-an-alias%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown