Multi line alias in bash
Clash Royale CLAN TAG#URR8PPP
up vote
4
down vote
favorite
I have the following script. It's a simple test case where a
is any string value and b
is supposed to be a path.
#!/bin/bash
alias jo "
echo "please enter values "
read a
read -e b
echo "My values are $a and $b""
However whenever I try to execute ./sample.sh I get the following errors:
./sample.sh: line 3: alias: jo: not found
./sample.sh: line 3: alias: echo please: not found
./sample.sh: line 3: alias: enter: not found
./sample.sh: line 3: alias: values: not found
./sample.sh: line 3: alias: read a read -e b echo My: not found
./sample.sh: line 3: alias: values: not found
./sample.sh: line 3: alias: are: not found
./sample.sh: line 3: alias: and: not found
./sample.sh: line 3: alias: : not found
and when I try source sample.sh
I get the following:
a: Undefined variable.
My aim was to make this an alias so that I can source this script and just run the alias to execute the line of commands. Can someone look at this and let me know what the error is?
command-line bash scripts alias
add a comment |Â
up vote
4
down vote
favorite
I have the following script. It's a simple test case where a
is any string value and b
is supposed to be a path.
#!/bin/bash
alias jo "
echo "please enter values "
read a
read -e b
echo "My values are $a and $b""
However whenever I try to execute ./sample.sh I get the following errors:
./sample.sh: line 3: alias: jo: not found
./sample.sh: line 3: alias: echo please: not found
./sample.sh: line 3: alias: enter: not found
./sample.sh: line 3: alias: values: not found
./sample.sh: line 3: alias: read a read -e b echo My: not found
./sample.sh: line 3: alias: values: not found
./sample.sh: line 3: alias: are: not found
./sample.sh: line 3: alias: and: not found
./sample.sh: line 3: alias: : not found
and when I try source sample.sh
I get the following:
a: Undefined variable.
My aim was to make this an alias so that I can source this script and just run the alias to execute the line of commands. Can someone look at this and let me know what the error is?
command-line bash scripts alias
3
When you think you need multiline and multiple-command alias, it's time to either define a function or make a script.
– Sergiy Kolodyazhnyy
Sep 2 at 2:22
@SergiyKolodyazhnyy noted that
– Jovin Miranda
Sep 6 at 19:27
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I have the following script. It's a simple test case where a
is any string value and b
is supposed to be a path.
#!/bin/bash
alias jo "
echo "please enter values "
read a
read -e b
echo "My values are $a and $b""
However whenever I try to execute ./sample.sh I get the following errors:
./sample.sh: line 3: alias: jo: not found
./sample.sh: line 3: alias: echo please: not found
./sample.sh: line 3: alias: enter: not found
./sample.sh: line 3: alias: values: not found
./sample.sh: line 3: alias: read a read -e b echo My: not found
./sample.sh: line 3: alias: values: not found
./sample.sh: line 3: alias: are: not found
./sample.sh: line 3: alias: and: not found
./sample.sh: line 3: alias: : not found
and when I try source sample.sh
I get the following:
a: Undefined variable.
My aim was to make this an alias so that I can source this script and just run the alias to execute the line of commands. Can someone look at this and let me know what the error is?
command-line bash scripts alias
I have the following script. It's a simple test case where a
is any string value and b
is supposed to be a path.
#!/bin/bash
alias jo "
echo "please enter values "
read a
read -e b
echo "My values are $a and $b""
However whenever I try to execute ./sample.sh I get the following errors:
./sample.sh: line 3: alias: jo: not found
./sample.sh: line 3: alias: echo please: not found
./sample.sh: line 3: alias: enter: not found
./sample.sh: line 3: alias: values: not found
./sample.sh: line 3: alias: read a read -e b echo My: not found
./sample.sh: line 3: alias: values: not found
./sample.sh: line 3: alias: are: not found
./sample.sh: line 3: alias: and: not found
./sample.sh: line 3: alias: : not found
and when I try source sample.sh
I get the following:
a: Undefined variable.
My aim was to make this an alias so that I can source this script and just run the alias to execute the line of commands. Can someone look at this and let me know what the error is?
command-line bash scripts alias
edited Sep 2 at 7:43
RonJohn
363110
363110
asked Sep 2 at 2:05
Jovin Miranda
374
374
3
When you think you need multiline and multiple-command alias, it's time to either define a function or make a script.
– Sergiy Kolodyazhnyy
Sep 2 at 2:22
@SergiyKolodyazhnyy noted that
– Jovin Miranda
Sep 6 at 19:27
add a comment |Â
3
When you think you need multiline and multiple-command alias, it's time to either define a function or make a script.
– Sergiy Kolodyazhnyy
Sep 2 at 2:22
@SergiyKolodyazhnyy noted that
– Jovin Miranda
Sep 6 at 19:27
3
3
When you think you need multiline and multiple-command alias, it's time to either define a function or make a script.
– Sergiy Kolodyazhnyy
Sep 2 at 2:22
When you think you need multiline and multiple-command alias, it's time to either define a function or make a script.
– Sergiy Kolodyazhnyy
Sep 2 at 2:22
@SergiyKolodyazhnyy noted that
– Jovin Miranda
Sep 6 at 19:27
@SergiyKolodyazhnyy noted that
– Jovin Miranda
Sep 6 at 19:27
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
8
down vote
accepted
You have a couple of issues here
unlike in
csh
, inbash
(and other Bourne-like shells), aliases are assigned with an=
sign e.g.alias foo=bar
quotes can't be nested like that; in this case, you can use single quotes around the alias and double quotes inside
the backslash
is a line continuation character: syntactically, it makes your command into a single line (the opposite of what you want)
So
#!/bin/bash
alias jo='
echo "please enter values "
read a
read -e b
echo "My values are $a and $b"'
Testing: first we source the file:
$ . ./myscript.sh
then
$ jo
please enter values
foo bar
baz
My values are foo bar and baz
If you want to use the alias within a script, then remember that aliases are only enabled by default in interactive shells: to enable them inside a script you will need to add
shopt -s expand_aliases
Regardless of everything above, you should consider using a shell function rather than an alias for things like this
1
Another reason to use single quotes around the alias is that variables inside double quotes are expanded, so$a
and$b
would be expanded at definition time, not when the alias is executed.
– Barmar
Sep 2 at 12:23
Thanks for the solution, the issue here is despite me writing the script as given by you and when i execute it i get jo: command not found the alias for some reason doesnt get registered. when i source the file then i get this error Unmatched " . the only reason i am doing alias and not function is cause i want to call this line of code at command line and in function i dont know how to achieve that. i have seen the function call is usually within the script. Usage of alias was just to ensure i can use it like a function and call it whenever i want it
– Jovin Miranda
Sep 3 at 3:46
anyways fixed it, i was using default shell as tcsh hence the problem was coming.
– Jovin Miranda
Sep 6 at 19:25
add a comment |Â
up vote
5
down vote
Get used to using functions in the POSIX-type shell. You don't have any of the quoting issues:
jo ()
read -p "Enter value for 'a': " -e a
read -p "Enter value for 'b': " -e b
echo "My values are $a and $b"
Thanks for the solution, this did work when i call the function within the script. How do i call the function from command line itself, cause i have multiple such scripts and would rather want a situation where i write all the functions in one file but call them from command line instead. That was the whole reason i went for alias
– Jovin Miranda
Sep 3 at 3:50
It's exactly the same. Put the functions in a file and source that file
– glenn jackman
Sep 3 at 4:04
got it what i actually did is use the foll. command "$@" so that i could call the function name while executing the file
– Jovin Miranda
Sep 6 at 19:26
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
accepted
You have a couple of issues here
unlike in
csh
, inbash
(and other Bourne-like shells), aliases are assigned with an=
sign e.g.alias foo=bar
quotes can't be nested like that; in this case, you can use single quotes around the alias and double quotes inside
the backslash
is a line continuation character: syntactically, it makes your command into a single line (the opposite of what you want)
So
#!/bin/bash
alias jo='
echo "please enter values "
read a
read -e b
echo "My values are $a and $b"'
Testing: first we source the file:
$ . ./myscript.sh
then
$ jo
please enter values
foo bar
baz
My values are foo bar and baz
If you want to use the alias within a script, then remember that aliases are only enabled by default in interactive shells: to enable them inside a script you will need to add
shopt -s expand_aliases
Regardless of everything above, you should consider using a shell function rather than an alias for things like this
1
Another reason to use single quotes around the alias is that variables inside double quotes are expanded, so$a
and$b
would be expanded at definition time, not when the alias is executed.
– Barmar
Sep 2 at 12:23
Thanks for the solution, the issue here is despite me writing the script as given by you and when i execute it i get jo: command not found the alias for some reason doesnt get registered. when i source the file then i get this error Unmatched " . the only reason i am doing alias and not function is cause i want to call this line of code at command line and in function i dont know how to achieve that. i have seen the function call is usually within the script. Usage of alias was just to ensure i can use it like a function and call it whenever i want it
– Jovin Miranda
Sep 3 at 3:46
anyways fixed it, i was using default shell as tcsh hence the problem was coming.
– Jovin Miranda
Sep 6 at 19:25
add a comment |Â
up vote
8
down vote
accepted
You have a couple of issues here
unlike in
csh
, inbash
(and other Bourne-like shells), aliases are assigned with an=
sign e.g.alias foo=bar
quotes can't be nested like that; in this case, you can use single quotes around the alias and double quotes inside
the backslash
is a line continuation character: syntactically, it makes your command into a single line (the opposite of what you want)
So
#!/bin/bash
alias jo='
echo "please enter values "
read a
read -e b
echo "My values are $a and $b"'
Testing: first we source the file:
$ . ./myscript.sh
then
$ jo
please enter values
foo bar
baz
My values are foo bar and baz
If you want to use the alias within a script, then remember that aliases are only enabled by default in interactive shells: to enable them inside a script you will need to add
shopt -s expand_aliases
Regardless of everything above, you should consider using a shell function rather than an alias for things like this
1
Another reason to use single quotes around the alias is that variables inside double quotes are expanded, so$a
and$b
would be expanded at definition time, not when the alias is executed.
– Barmar
Sep 2 at 12:23
Thanks for the solution, the issue here is despite me writing the script as given by you and when i execute it i get jo: command not found the alias for some reason doesnt get registered. when i source the file then i get this error Unmatched " . the only reason i am doing alias and not function is cause i want to call this line of code at command line and in function i dont know how to achieve that. i have seen the function call is usually within the script. Usage of alias was just to ensure i can use it like a function and call it whenever i want it
– Jovin Miranda
Sep 3 at 3:46
anyways fixed it, i was using default shell as tcsh hence the problem was coming.
– Jovin Miranda
Sep 6 at 19:25
add a comment |Â
up vote
8
down vote
accepted
up vote
8
down vote
accepted
You have a couple of issues here
unlike in
csh
, inbash
(and other Bourne-like shells), aliases are assigned with an=
sign e.g.alias foo=bar
quotes can't be nested like that; in this case, you can use single quotes around the alias and double quotes inside
the backslash
is a line continuation character: syntactically, it makes your command into a single line (the opposite of what you want)
So
#!/bin/bash
alias jo='
echo "please enter values "
read a
read -e b
echo "My values are $a and $b"'
Testing: first we source the file:
$ . ./myscript.sh
then
$ jo
please enter values
foo bar
baz
My values are foo bar and baz
If you want to use the alias within a script, then remember that aliases are only enabled by default in interactive shells: to enable them inside a script you will need to add
shopt -s expand_aliases
Regardless of everything above, you should consider using a shell function rather than an alias for things like this
You have a couple of issues here
unlike in
csh
, inbash
(and other Bourne-like shells), aliases are assigned with an=
sign e.g.alias foo=bar
quotes can't be nested like that; in this case, you can use single quotes around the alias and double quotes inside
the backslash
is a line continuation character: syntactically, it makes your command into a single line (the opposite of what you want)
So
#!/bin/bash
alias jo='
echo "please enter values "
read a
read -e b
echo "My values are $a and $b"'
Testing: first we source the file:
$ . ./myscript.sh
then
$ jo
please enter values
foo bar
baz
My values are foo bar and baz
If you want to use the alias within a script, then remember that aliases are only enabled by default in interactive shells: to enable them inside a script you will need to add
shopt -s expand_aliases
Regardless of everything above, you should consider using a shell function rather than an alias for things like this
answered Sep 2 at 2:20
steeldriver
62.8k1197165
62.8k1197165
1
Another reason to use single quotes around the alias is that variables inside double quotes are expanded, so$a
and$b
would be expanded at definition time, not when the alias is executed.
– Barmar
Sep 2 at 12:23
Thanks for the solution, the issue here is despite me writing the script as given by you and when i execute it i get jo: command not found the alias for some reason doesnt get registered. when i source the file then i get this error Unmatched " . the only reason i am doing alias and not function is cause i want to call this line of code at command line and in function i dont know how to achieve that. i have seen the function call is usually within the script. Usage of alias was just to ensure i can use it like a function and call it whenever i want it
– Jovin Miranda
Sep 3 at 3:46
anyways fixed it, i was using default shell as tcsh hence the problem was coming.
– Jovin Miranda
Sep 6 at 19:25
add a comment |Â
1
Another reason to use single quotes around the alias is that variables inside double quotes are expanded, so$a
and$b
would be expanded at definition time, not when the alias is executed.
– Barmar
Sep 2 at 12:23
Thanks for the solution, the issue here is despite me writing the script as given by you and when i execute it i get jo: command not found the alias for some reason doesnt get registered. when i source the file then i get this error Unmatched " . the only reason i am doing alias and not function is cause i want to call this line of code at command line and in function i dont know how to achieve that. i have seen the function call is usually within the script. Usage of alias was just to ensure i can use it like a function and call it whenever i want it
– Jovin Miranda
Sep 3 at 3:46
anyways fixed it, i was using default shell as tcsh hence the problem was coming.
– Jovin Miranda
Sep 6 at 19:25
1
1
Another reason to use single quotes around the alias is that variables inside double quotes are expanded, so
$a
and $b
would be expanded at definition time, not when the alias is executed.– Barmar
Sep 2 at 12:23
Another reason to use single quotes around the alias is that variables inside double quotes are expanded, so
$a
and $b
would be expanded at definition time, not when the alias is executed.– Barmar
Sep 2 at 12:23
Thanks for the solution, the issue here is despite me writing the script as given by you and when i execute it i get jo: command not found the alias for some reason doesnt get registered. when i source the file then i get this error Unmatched " . the only reason i am doing alias and not function is cause i want to call this line of code at command line and in function i dont know how to achieve that. i have seen the function call is usually within the script. Usage of alias was just to ensure i can use it like a function and call it whenever i want it
– Jovin Miranda
Sep 3 at 3:46
Thanks for the solution, the issue here is despite me writing the script as given by you and when i execute it i get jo: command not found the alias for some reason doesnt get registered. when i source the file then i get this error Unmatched " . the only reason i am doing alias and not function is cause i want to call this line of code at command line and in function i dont know how to achieve that. i have seen the function call is usually within the script. Usage of alias was just to ensure i can use it like a function and call it whenever i want it
– Jovin Miranda
Sep 3 at 3:46
anyways fixed it, i was using default shell as tcsh hence the problem was coming.
– Jovin Miranda
Sep 6 at 19:25
anyways fixed it, i was using default shell as tcsh hence the problem was coming.
– Jovin Miranda
Sep 6 at 19:25
add a comment |Â
up vote
5
down vote
Get used to using functions in the POSIX-type shell. You don't have any of the quoting issues:
jo ()
read -p "Enter value for 'a': " -e a
read -p "Enter value for 'b': " -e b
echo "My values are $a and $b"
Thanks for the solution, this did work when i call the function within the script. How do i call the function from command line itself, cause i have multiple such scripts and would rather want a situation where i write all the functions in one file but call them from command line instead. That was the whole reason i went for alias
– Jovin Miranda
Sep 3 at 3:50
It's exactly the same. Put the functions in a file and source that file
– glenn jackman
Sep 3 at 4:04
got it what i actually did is use the foll. command "$@" so that i could call the function name while executing the file
– Jovin Miranda
Sep 6 at 19:26
add a comment |Â
up vote
5
down vote
Get used to using functions in the POSIX-type shell. You don't have any of the quoting issues:
jo ()
read -p "Enter value for 'a': " -e a
read -p "Enter value for 'b': " -e b
echo "My values are $a and $b"
Thanks for the solution, this did work when i call the function within the script. How do i call the function from command line itself, cause i have multiple such scripts and would rather want a situation where i write all the functions in one file but call them from command line instead. That was the whole reason i went for alias
– Jovin Miranda
Sep 3 at 3:50
It's exactly the same. Put the functions in a file and source that file
– glenn jackman
Sep 3 at 4:04
got it what i actually did is use the foll. command "$@" so that i could call the function name while executing the file
– Jovin Miranda
Sep 6 at 19:26
add a comment |Â
up vote
5
down vote
up vote
5
down vote
Get used to using functions in the POSIX-type shell. You don't have any of the quoting issues:
jo ()
read -p "Enter value for 'a': " -e a
read -p "Enter value for 'b': " -e b
echo "My values are $a and $b"
Get used to using functions in the POSIX-type shell. You don't have any of the quoting issues:
jo ()
read -p "Enter value for 'a': " -e a
read -p "Enter value for 'b': " -e b
echo "My values are $a and $b"
answered Sep 2 at 4:11
glenn jackman
11.8k2341
11.8k2341
Thanks for the solution, this did work when i call the function within the script. How do i call the function from command line itself, cause i have multiple such scripts and would rather want a situation where i write all the functions in one file but call them from command line instead. That was the whole reason i went for alias
– Jovin Miranda
Sep 3 at 3:50
It's exactly the same. Put the functions in a file and source that file
– glenn jackman
Sep 3 at 4:04
got it what i actually did is use the foll. command "$@" so that i could call the function name while executing the file
– Jovin Miranda
Sep 6 at 19:26
add a comment |Â
Thanks for the solution, this did work when i call the function within the script. How do i call the function from command line itself, cause i have multiple such scripts and would rather want a situation where i write all the functions in one file but call them from command line instead. That was the whole reason i went for alias
– Jovin Miranda
Sep 3 at 3:50
It's exactly the same. Put the functions in a file and source that file
– glenn jackman
Sep 3 at 4:04
got it what i actually did is use the foll. command "$@" so that i could call the function name while executing the file
– Jovin Miranda
Sep 6 at 19:26
Thanks for the solution, this did work when i call the function within the script. How do i call the function from command line itself, cause i have multiple such scripts and would rather want a situation where i write all the functions in one file but call them from command line instead. That was the whole reason i went for alias
– Jovin Miranda
Sep 3 at 3:50
Thanks for the solution, this did work when i call the function within the script. How do i call the function from command line itself, cause i have multiple such scripts and would rather want a situation where i write all the functions in one file but call them from command line instead. That was the whole reason i went for alias
– Jovin Miranda
Sep 3 at 3:50
It's exactly the same. Put the functions in a file and source that file
– glenn jackman
Sep 3 at 4:04
It's exactly the same. Put the functions in a file and source that file
– glenn jackman
Sep 3 at 4:04
got it what i actually did is use the foll. command "$@" so that i could call the function name while executing the file
– Jovin Miranda
Sep 6 at 19:26
got it what i actually did is use the foll. command "$@" so that i could call the function name while executing the file
– Jovin Miranda
Sep 6 at 19:26
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1071263%2fmulti-line-alias-in-bash%23new-answer', 'question_page');
);
Post as a guest
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
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
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
3
When you think you need multiline and multiple-command alias, it's time to either define a function or make a script.
– Sergiy Kolodyazhnyy
Sep 2 at 2:22
@SergiyKolodyazhnyy noted that
– Jovin Miranda
Sep 6 at 19:27