Is there an equivalent to PowerShell's âSwitchâ in bash?
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I was curious as to whether or not there is an equivalent to PowerShell's "Switch" command that lets you maneuver with input instead of using a multitude of "if-statements"
bash powershell
add a comment |Â
up vote
2
down vote
favorite
I was curious as to whether or not there is an equivalent to PowerShell's "Switch" command that lets you maneuver with input instead of using a multitude of "if-statements"
bash powershell
If only there were such a construct in Python. Alas.
â DopeGhoti
Apr 19 at 18:48
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I was curious as to whether or not there is an equivalent to PowerShell's "Switch" command that lets you maneuver with input instead of using a multitude of "if-statements"
bash powershell
I was curious as to whether or not there is an equivalent to PowerShell's "Switch" command that lets you maneuver with input instead of using a multitude of "if-statements"
bash powershell
asked Apr 19 at 18:25
Paco R.
111
111
If only there were such a construct in Python. Alas.
â DopeGhoti
Apr 19 at 18:48
add a comment |Â
If only there were such a construct in Python. Alas.
â DopeGhoti
Apr 19 at 18:48
If only there were such a construct in Python. Alas.
â DopeGhoti
Apr 19 at 18:48
If only there were such a construct in Python. Alas.
â DopeGhoti
Apr 19 at 18:48
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
11
down vote
If you're looking at a variable to determine flow, you want to use a case
statement.
case "$var" in
val1)
do_something
;;
val2)
do_something_else
;;
esac
If you're looking to interactively get user input, you want to also use a select
statement.
select action in proceed ponder perspire quit; do
case "$action" in
proceed)
go_on_then
;;
ponder)
have_a_think
;;
perspire)
exude_salty_secretions
;;
quit)
break
;;
esac
done
1
Worth noting that it's matching against glob patterns. For example, if you write[Qq]uit|exit|go-*)
. It'd match "Quit", "quit", "exit", and anything beginning with "go-" like "go-elsewhere".default
would be*)
, which is simply a glob that matches anything.
â JoL
Apr 19 at 22:51
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
11
down vote
If you're looking at a variable to determine flow, you want to use a case
statement.
case "$var" in
val1)
do_something
;;
val2)
do_something_else
;;
esac
If you're looking to interactively get user input, you want to also use a select
statement.
select action in proceed ponder perspire quit; do
case "$action" in
proceed)
go_on_then
;;
ponder)
have_a_think
;;
perspire)
exude_salty_secretions
;;
quit)
break
;;
esac
done
1
Worth noting that it's matching against glob patterns. For example, if you write[Qq]uit|exit|go-*)
. It'd match "Quit", "quit", "exit", and anything beginning with "go-" like "go-elsewhere".default
would be*)
, which is simply a glob that matches anything.
â JoL
Apr 19 at 22:51
add a comment |Â
up vote
11
down vote
If you're looking at a variable to determine flow, you want to use a case
statement.
case "$var" in
val1)
do_something
;;
val2)
do_something_else
;;
esac
If you're looking to interactively get user input, you want to also use a select
statement.
select action in proceed ponder perspire quit; do
case "$action" in
proceed)
go_on_then
;;
ponder)
have_a_think
;;
perspire)
exude_salty_secretions
;;
quit)
break
;;
esac
done
1
Worth noting that it's matching against glob patterns. For example, if you write[Qq]uit|exit|go-*)
. It'd match "Quit", "quit", "exit", and anything beginning with "go-" like "go-elsewhere".default
would be*)
, which is simply a glob that matches anything.
â JoL
Apr 19 at 22:51
add a comment |Â
up vote
11
down vote
up vote
11
down vote
If you're looking at a variable to determine flow, you want to use a case
statement.
case "$var" in
val1)
do_something
;;
val2)
do_something_else
;;
esac
If you're looking to interactively get user input, you want to also use a select
statement.
select action in proceed ponder perspire quit; do
case "$action" in
proceed)
go_on_then
;;
ponder)
have_a_think
;;
perspire)
exude_salty_secretions
;;
quit)
break
;;
esac
done
If you're looking at a variable to determine flow, you want to use a case
statement.
case "$var" in
val1)
do_something
;;
val2)
do_something_else
;;
esac
If you're looking to interactively get user input, you want to also use a select
statement.
select action in proceed ponder perspire quit; do
case "$action" in
proceed)
go_on_then
;;
ponder)
have_a_think
;;
perspire)
exude_salty_secretions
;;
quit)
break
;;
esac
done
answered Apr 19 at 18:33
DopeGhoti
40k54779
40k54779
1
Worth noting that it's matching against glob patterns. For example, if you write[Qq]uit|exit|go-*)
. It'd match "Quit", "quit", "exit", and anything beginning with "go-" like "go-elsewhere".default
would be*)
, which is simply a glob that matches anything.
â JoL
Apr 19 at 22:51
add a comment |Â
1
Worth noting that it's matching against glob patterns. For example, if you write[Qq]uit|exit|go-*)
. It'd match "Quit", "quit", "exit", and anything beginning with "go-" like "go-elsewhere".default
would be*)
, which is simply a glob that matches anything.
â JoL
Apr 19 at 22:51
1
1
Worth noting that it's matching against glob patterns. For example, if you write
[Qq]uit|exit|go-*)
. It'd match "Quit", "quit", "exit", and anything beginning with "go-" like "go-elsewhere". default
would be *)
, which is simply a glob that matches anything.â JoL
Apr 19 at 22:51
Worth noting that it's matching against glob patterns. For example, if you write
[Qq]uit|exit|go-*)
. It'd match "Quit", "quit", "exit", and anything beginning with "go-" like "go-elsewhere". default
would be *)
, which is simply a glob that matches anything.â JoL
Apr 19 at 22:51
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%2funix.stackexchange.com%2fquestions%2f438794%2fis-there-an-equivalent-to-powershells-switch-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
If only there were such a construct in Python. Alas.
â DopeGhoti
Apr 19 at 18:48