LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Grouping commands in bash (https://www.linuxquestions.org/questions/linux-newbie-8/grouping-commands-in-bash-4175441097/)

trunikov 12-12-2012 04:09 AM

Grouping commands in bash
 
Hi ALL,

I've question about grouping of commands in the bash.
For the sake of simplicity I'll show a bit artificial example but it explain the issue.
For example I can run following commands:

Code:

$ date; date
Wed Dec 12 11:52:46 EET 2012
Wed Dec 12 11:52:46 EET 2012

Excellent, but now I would like to run the same commands in different time zone:

Code:

$ TZ='Asia/Kolkata' date; date
Wed Dec 12 15:24:07 IST 2012
Wed Dec 12 11:54:07 EET 2012

Not perfect, the result is not exactly what I want. The first command executed in the specified timezone but the second one is executed in default system timezone. But I would like to run both commands in the specified timezone.
I know that bash allows grouping of executed commands like below:

Code:

$ (date; date)
Wed Dec 12 11:58:56 EET 2012
Wed Dec 12 11:58:56 EET 2012

Unfortunately in this syntax I can't specify timezone:

Code:

$ TZ='Asia/Kolkata' (date; date)
bash: syntax error near unexpected token `('

So, can anybody help me and show correct syntax of command line.

Thanks.

P.S. I know that I can export variable TZ to environment and then execute commands from the example, but I want to find out how to do that with help of a single command line :cool:

chrism01 12-12-2012 04:34 AM

This ?
Code:

(export TZ='Asia/Kolkata' && date && date)
creates a subshell, so your parent shell retains orig TZ (if that's what you want)

trunikov 12-12-2012 04:39 AM

Quote:

Originally Posted by chrism01 (Post 4847596)
This ?
Code:

(export TZ='Asia/Kolkata' && date && date)
creates a subshell, so your parent shell retains orig TZ (if that's what you want)

Hi chrism01,

Thank you very much. Your answer give me right direction to write what I want:

Code:

$ (TZ='Asia/Kolkata'; date; date)
Wed Dec 12 12:36:43 EET 2012
Wed Dec 12 12:36:43 EET 2012

Many thanks.


All times are GMT -5. The time now is 02:02 AM.