Create an abbreviation for â2>/dev/null &â
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I'm trying to compose myself a bash script with the intent of it being used commonly in a terminal. It should start an application as a background process and discard it's stderr output. Here's what I got:
for app in $@; do
$app 2>/dev/null
done
It seemed to work just fine with bare applications started without parameters, like script.sh firefox gedit
but failed to do the following:
script.sh "vlc video.mp4"
My question is: How can I enhance this basic script to handle applications which take parameters/files as their input? Maybe there already is a tool I can use?
bash shell-script command-line
add a comment |Â
up vote
0
down vote
favorite
I'm trying to compose myself a bash script with the intent of it being used commonly in a terminal. It should start an application as a background process and discard it's stderr output. Here's what I got:
for app in $@; do
$app 2>/dev/null
done
It seemed to work just fine with bare applications started without parameters, like script.sh firefox gedit
but failed to do the following:
script.sh "vlc video.mp4"
My question is: How can I enhance this basic script to handle applications which take parameters/files as their input? Maybe there already is a tool I can use?
bash shell-script command-line
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to compose myself a bash script with the intent of it being used commonly in a terminal. It should start an application as a background process and discard it's stderr output. Here's what I got:
for app in $@; do
$app 2>/dev/null
done
It seemed to work just fine with bare applications started without parameters, like script.sh firefox gedit
but failed to do the following:
script.sh "vlc video.mp4"
My question is: How can I enhance this basic script to handle applications which take parameters/files as their input? Maybe there already is a tool I can use?
bash shell-script command-line
I'm trying to compose myself a bash script with the intent of it being used commonly in a terminal. It should start an application as a background process and discard it's stderr output. Here's what I got:
for app in $@; do
$app 2>/dev/null
done
It seemed to work just fine with bare applications started without parameters, like script.sh firefox gedit
but failed to do the following:
script.sh "vlc video.mp4"
My question is: How can I enhance this basic script to handle applications which take parameters/files as their input? Maybe there already is a tool I can use?
bash shell-script command-line
asked May 3 at 13:02
610
32
32
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
5
down vote
accepted
There are two issues:
The use of
$@
without quotes.This would make the loop iterate over
vlc
andvideo.mp4
as two separate items, even if these were within the same quoted string in the invocation of the script.Using a command in a variable.
If the command is anything more complicated than a single simple command, then this won't work. You would have to
eval
the given string instead.
Taking this into account, your script could look like
#!/bin/sh
for cmd do # or: for cmd in "$@"; do
eval "$cmd" 2>/dev/null
done
Calling this as
./script 'echo "hello world"' 'vim "$HOME/.profile"' 'tr a-z A-Z <"$HOME/.profile" | grep -c EXPORT'
would first run echo "hello world"
, and when that finishes, it would open vim
for editing the named file in that second command. The last command is more complex but handled by the fact that we use eval
(it just changes all alphabetic characters to uppercase and counts the number of times the string EXPORT
occurs in a file). It is run as soon as the vim
session exits.
With this, you could even do
./script 'd=$HOME' 'ls "$d"'
i.e., set variables that are used in later commands. This works because the commands invoked by eval
are run in the same environment as the script. This would not work if you start the commands as background tasks though, as the title of your question suggests.
Thank you for the clarifying answer, I'll definitely make use of 'eval' now that I know about it.
â 610
May 4 at 11:18
add a comment |Â
up vote
1
down vote
You're currently telling your script to handle each parameter as a separate application. So you are running:
$ vlc
Then:
$ video.mp4
You should make your script more like this:
#!/bin/bash
$@ 2>/dev/null
Which, in my opinion, would be better suited as a function:
func_name()
$@ 2>/dev/null
Note: this will only allow you to run one command at a time. I'm not sure if your original intent was to pass multiple commands at once to your script, if so I will delete.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
There are two issues:
The use of
$@
without quotes.This would make the loop iterate over
vlc
andvideo.mp4
as two separate items, even if these were within the same quoted string in the invocation of the script.Using a command in a variable.
If the command is anything more complicated than a single simple command, then this won't work. You would have to
eval
the given string instead.
Taking this into account, your script could look like
#!/bin/sh
for cmd do # or: for cmd in "$@"; do
eval "$cmd" 2>/dev/null
done
Calling this as
./script 'echo "hello world"' 'vim "$HOME/.profile"' 'tr a-z A-Z <"$HOME/.profile" | grep -c EXPORT'
would first run echo "hello world"
, and when that finishes, it would open vim
for editing the named file in that second command. The last command is more complex but handled by the fact that we use eval
(it just changes all alphabetic characters to uppercase and counts the number of times the string EXPORT
occurs in a file). It is run as soon as the vim
session exits.
With this, you could even do
./script 'd=$HOME' 'ls "$d"'
i.e., set variables that are used in later commands. This works because the commands invoked by eval
are run in the same environment as the script. This would not work if you start the commands as background tasks though, as the title of your question suggests.
Thank you for the clarifying answer, I'll definitely make use of 'eval' now that I know about it.
â 610
May 4 at 11:18
add a comment |Â
up vote
5
down vote
accepted
There are two issues:
The use of
$@
without quotes.This would make the loop iterate over
vlc
andvideo.mp4
as two separate items, even if these were within the same quoted string in the invocation of the script.Using a command in a variable.
If the command is anything more complicated than a single simple command, then this won't work. You would have to
eval
the given string instead.
Taking this into account, your script could look like
#!/bin/sh
for cmd do # or: for cmd in "$@"; do
eval "$cmd" 2>/dev/null
done
Calling this as
./script 'echo "hello world"' 'vim "$HOME/.profile"' 'tr a-z A-Z <"$HOME/.profile" | grep -c EXPORT'
would first run echo "hello world"
, and when that finishes, it would open vim
for editing the named file in that second command. The last command is more complex but handled by the fact that we use eval
(it just changes all alphabetic characters to uppercase and counts the number of times the string EXPORT
occurs in a file). It is run as soon as the vim
session exits.
With this, you could even do
./script 'd=$HOME' 'ls "$d"'
i.e., set variables that are used in later commands. This works because the commands invoked by eval
are run in the same environment as the script. This would not work if you start the commands as background tasks though, as the title of your question suggests.
Thank you for the clarifying answer, I'll definitely make use of 'eval' now that I know about it.
â 610
May 4 at 11:18
add a comment |Â
up vote
5
down vote
accepted
up vote
5
down vote
accepted
There are two issues:
The use of
$@
without quotes.This would make the loop iterate over
vlc
andvideo.mp4
as two separate items, even if these were within the same quoted string in the invocation of the script.Using a command in a variable.
If the command is anything more complicated than a single simple command, then this won't work. You would have to
eval
the given string instead.
Taking this into account, your script could look like
#!/bin/sh
for cmd do # or: for cmd in "$@"; do
eval "$cmd" 2>/dev/null
done
Calling this as
./script 'echo "hello world"' 'vim "$HOME/.profile"' 'tr a-z A-Z <"$HOME/.profile" | grep -c EXPORT'
would first run echo "hello world"
, and when that finishes, it would open vim
for editing the named file in that second command. The last command is more complex but handled by the fact that we use eval
(it just changes all alphabetic characters to uppercase and counts the number of times the string EXPORT
occurs in a file). It is run as soon as the vim
session exits.
With this, you could even do
./script 'd=$HOME' 'ls "$d"'
i.e., set variables that are used in later commands. This works because the commands invoked by eval
are run in the same environment as the script. This would not work if you start the commands as background tasks though, as the title of your question suggests.
There are two issues:
The use of
$@
without quotes.This would make the loop iterate over
vlc
andvideo.mp4
as two separate items, even if these were within the same quoted string in the invocation of the script.Using a command in a variable.
If the command is anything more complicated than a single simple command, then this won't work. You would have to
eval
the given string instead.
Taking this into account, your script could look like
#!/bin/sh
for cmd do # or: for cmd in "$@"; do
eval "$cmd" 2>/dev/null
done
Calling this as
./script 'echo "hello world"' 'vim "$HOME/.profile"' 'tr a-z A-Z <"$HOME/.profile" | grep -c EXPORT'
would first run echo "hello world"
, and when that finishes, it would open vim
for editing the named file in that second command. The last command is more complex but handled by the fact that we use eval
(it just changes all alphabetic characters to uppercase and counts the number of times the string EXPORT
occurs in a file). It is run as soon as the vim
session exits.
With this, you could even do
./script 'd=$HOME' 'ls "$d"'
i.e., set variables that are used in later commands. This works because the commands invoked by eval
are run in the same environment as the script. This would not work if you start the commands as background tasks though, as the title of your question suggests.
edited May 3 at 13:52
answered May 3 at 13:08
Kusalananda
102k13199316
102k13199316
Thank you for the clarifying answer, I'll definitely make use of 'eval' now that I know about it.
â 610
May 4 at 11:18
add a comment |Â
Thank you for the clarifying answer, I'll definitely make use of 'eval' now that I know about it.
â 610
May 4 at 11:18
Thank you for the clarifying answer, I'll definitely make use of 'eval' now that I know about it.
â 610
May 4 at 11:18
Thank you for the clarifying answer, I'll definitely make use of 'eval' now that I know about it.
â 610
May 4 at 11:18
add a comment |Â
up vote
1
down vote
You're currently telling your script to handle each parameter as a separate application. So you are running:
$ vlc
Then:
$ video.mp4
You should make your script more like this:
#!/bin/bash
$@ 2>/dev/null
Which, in my opinion, would be better suited as a function:
func_name()
$@ 2>/dev/null
Note: this will only allow you to run one command at a time. I'm not sure if your original intent was to pass multiple commands at once to your script, if so I will delete.
add a comment |Â
up vote
1
down vote
You're currently telling your script to handle each parameter as a separate application. So you are running:
$ vlc
Then:
$ video.mp4
You should make your script more like this:
#!/bin/bash
$@ 2>/dev/null
Which, in my opinion, would be better suited as a function:
func_name()
$@ 2>/dev/null
Note: this will only allow you to run one command at a time. I'm not sure if your original intent was to pass multiple commands at once to your script, if so I will delete.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You're currently telling your script to handle each parameter as a separate application. So you are running:
$ vlc
Then:
$ video.mp4
You should make your script more like this:
#!/bin/bash
$@ 2>/dev/null
Which, in my opinion, would be better suited as a function:
func_name()
$@ 2>/dev/null
Note: this will only allow you to run one command at a time. I'm not sure if your original intent was to pass multiple commands at once to your script, if so I will delete.
You're currently telling your script to handle each parameter as a separate application. So you are running:
$ vlc
Then:
$ video.mp4
You should make your script more like this:
#!/bin/bash
$@ 2>/dev/null
Which, in my opinion, would be better suited as a function:
func_name()
$@ 2>/dev/null
Note: this will only allow you to run one command at a time. I'm not sure if your original intent was to pass multiple commands at once to your script, if so I will delete.
edited May 3 at 13:19
answered May 3 at 13:08
Jesse_b
10.3k22658
10.3k22658
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f441543%2fcreate-an-abbreviation-for-2-dev-null%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