Skip to content
Advertisement

How to open apps from bash (Linux)

Automate part of your life where you can! (It doesn’t seem to be working properly…). This time is for opening multiple apps on my desktop while I make my morning coffee.

I created a .sh to open my workspace apps like Visual Code, Slack, Firefox, etc.

The script is basically:

exec code
exec /usr/local/firefox_dev/firefox
exec snap/slack/39/usr/bin/slack
exec /snap/spotify/current/usr/share/spotify/spotify

I basically found the paths to the executable apps and put exec in front of them… And yes, I chmod +x ./workspace-apps-script.sh to make it executable.

The problem is: No matter which exec line I put first, it only executes the first line. I would like them to open one-by-one and open til the last one.

Advertisement

Answer

The problem is that you apps do not return when launched. So you have to let they go in background or detached.

To execute a command in background you need just to add & at the end of the command line in the script. For example:

#!/bin/bash

code &
/usr/local/firefox_dev/firefox &
snap/slack/39/usr/bin/slack &
/snap/spotify/current/usr/share/spotify/spotify &

If you want to execute the command in detached mode you can use nohup at the beginning of the command line. For example:

#!/bin/bash

nohup code
nohup /usr/local/firefox_dev/firefox
nohup snap/slack/39/usr/bin/slack
nohup /snap/spotify/current/usr/share/spotify/spotify

I suggest you to combine the two mode to get the best result. So you can do this in your script:

#!/bin/bash

nohup code &
nohup /usr/local/firefox_dev/firefox &
nohup snap/slack/39/usr/bin/slack &
nohup /snap/spotify/current/usr/share/spotify/spotify &

However if you want to execute this every time you login to you pc you can write the abobe lines directly in you .bashrc file That is in your home directory and is executed at every login.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement