Jump to content

Linux - Bash - Grouping Commands

Hi,

Is there a way to group multiple commands, which then can be executed by a single variable?

variable(

             command1

             command2

)

Any Questions?

Thank you

Link to comment
Share on other sites

Link to post
Share on other sites

You can create a bash script in order to do this.

 

For example a script with this placed in it. The first line of the script in necessary so that it finds the bash executable.

#/bin/bash

echo "Hello"
echo "World"

There are two separate commands here that can then be run by executing the script. Let's call this script hello. It will output the following

Hello
World

 

Now we need to make the script executable.

chmod 755 hello

 

Now this script can be executed by typing

./hello

However this only works in the current directory.

 

Make a directory for the script, and put it there.

mkdir bin
mv hello ~/bin/hello

 

Now you will want to edit your .bash_profile file to make it so that you can execute the script anywhere by simply typing hello.

 

Open ~/.bash_profile with your editor of choice and include the following command at the bottom of the file.

export PATH=$PATH:/home/user/bin

Some of these steps may require sudo privilege but otherwise, it's the same process.

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to comment
Share on other sites

Link to post
Share on other sites

Or did I misinterpret the complexity of your question?

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, DeadEyePsycho said:

You can create a bash script in order to do this.

 

For example a script with this placed in it. The first line of the script in necessary so that it finds the bash executable.


#/bin/bash

echo "Hello"
echo "World"

There are two separate commands here that can then be run by executing the script. Let's call this script hello. It will output the following


Hello
World

 

Now we need to make the script executable.


chmod 755 hello

 

Now this script can be executed by typing


./hello

However this only works in the current directory.

 

Make a directory for the script, and put it there.


mkdir bin
mv hello ~/bin/hello

 

Now you will want to edit your .bash_profile file to make it so that you can execute the script anywhere by simply typing hello.

 

Open ~/.bash_profile with your editor of choice and include the following command at the bottom of the file.


export PATH=$PATH:/home/user/bin

Some of these steps may require sudo privilege but otherwise, it's the same process.

 

I guess thats one way

Do you think this would work?

variable='cmd1;cmd2'

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, DeadEyePsycho said:

Or did I misinterpret the complexity of your question?

Not Really

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, spbr said:

I guess thats one way

Do you think this would work?

variable='cmd1;cmd2'

 

variable=$(cmd1 && cmd 2) will work.

 

 

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, DeadEyePsycho said:

variable=$(cmd1 && cmd 2) will work.

 

 

 

no quotations?

Link to comment
Share on other sites

Link to post
Share on other sites

12 minutes ago, spbr said:

no quotations?

From my usage, not when combining commands as it will cause the very next character after the first commands to be an argument for the first command. I don't do a lot bash scripting but that's what I've gathered while testing it.

 

@SCHISCHKA will likely know more than me.

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, DeadEyePsycho said:

From my usage, not when combining commands as it will cause the very next character after the first commands to be an argument for the first command. I don't do a lot bash scripting but that's what I've gathered while testing it.

 

@SCHISCHKA will likely know more than me.

 

Thank you

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, spbr said:

Hi,

Is there a way to group multiple commands, which then can be executed by a single variable?

variable(

             command1

             command2

)

Any Questions?

Thank you

yes you can do functions in bash

Quote

function functionName {

echo hello

ls /

echo "OPTIONAL: this put the first argument $1 that you pass into the function into the ls command"

ls $1

echo " this will put the second variable $2 into the find command"

find $2

}

then to execute

Quote

functionName argument1 argument2

 

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×