Combine two shell commands into one output in shell

I am trying to combine multiple commands into a single output.

#!/bin/bash x=$(date +%Y) x=$($x date +m%) echo "$x"

This returns

./ line 4: 2011: command not found

-------------Problems Reply------------

x=$(echo $(date +%Y) $(date +%m))

(Note that you've transposed the characters % and m in the month format.)

In the second line, you're trying to execute $x date +m%. At this point, $x will be set to the year, 2011. So now it's trying to run this command:

2011 date +%m

Which is not what you want.

You could either do this:

x=$(date +%Y)
y=$(date +%m)

echo "$x $y"

Or that:

x=$(date +%Y)
x="$x $(date +%m)"

echo "$x"

Or simply use the final date format straight away:

x=$(date "+%Y %m")
echo $x

Maybe this?


x=$(date +%Y)
x="$x$(date +%m)"

echo "$x"

...also correcting what appears to be a transpose in the format string you passed to date the second time around.

Semicolon to separate commands on the command line.

date +%Y ; date +m%

Or if you only want to run the second command if the first one succeeds, use double ampersand:

date +%Y && date +%m

Or if you want to run both commands simultaneously, mixing their output unpredictably (probably not what you want, but I thought I'd be thorough), use a single ampersand:

date +%Y & date +%m

echo ´date +%Y´ ´date +m%´

Note the reverse accent (´)

echo `date +%Y` `date +%m`

And to make the minimum change to the OP:

x=$(date +%Y)
x="$x $(date +%m)"
echo "$x"

Category:bash Views:0 Time:2011-02-13
Tags: shell bash

Related post

  • How can I set the encoding of shell-command-on-region output? 2009-12-01

    I have a small elisp script which applies Perl::Tidy on region or whole file. For reference, here's the script (borrowed from EmacsWiki): (defun perltidy-command(start end) "The perltidy command we pass markers to." (shell-command-on-region start end

  • OpenOffice Calc Macro: Run shell command and return output as result of custom function 2010-06-07

    I would like to write a custom OpenOffice function that runs a shell command and puts the result into the cell from which it was invoked. I have a basic macro working, but I can't find a way to capture the command's output. Function MyTest( c1 ) MyTe

  • php executing shell command gives random output in only in heavy use mode 2012-03-28

    Please help me with this issue that I am facing on production server. I launched an application that saves images in a directory only after checking that it does not already exist. To check if it already exists I used the following command find $file

  • Python calling shell commands. What type of shell is started? 2012-03-02

    I have the following python function that allows me to run shell commands from within a python script: import subprocess def run_shell_command(cmd,cwd=None): retVal = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, cwd=cwd); retVal = retVal

  • Run adb shell commands from script without exiting shell 2011-11-25

    I'm trying to create a script to simplify data checking with sqlite. For me it's quite troublesome to write : adb shell sqlite /data/data/ every time I want to check something in the db (and in the beginning phas

  • run a unix shell command if the output doesn't have a specific number of lines 2009-08-25

    I've got a unix command sequence that goes something like: command1 | command2 | command3 | wc -l Now that I have the number of lines, I'd like to do something (run a specific command with no inputs) if the number of lines isn't equal to a specific n

  • Is there a way to capture the collection of shell commands that fabric outputs? 2011-08-02

    I've got a collection of fab files that I use to manage servers, install applications on them etc for clients. Some of them are not very happy when I deliver install documentation as a fabfile and readme. There are usually experienced sysadmins, just

  • How to have colors in the output of (emacs) shell-command? 2011-01-18

    When executing the command shell-command, the output shown in the associated buffer is not colorized. This is particularly annoying when calling a testing framework (outputting yellow/green/red...) from within emacs. How can I configure, or extend, e

  • Gathering output from a Java app and shell command 2011-03-18

    I'm writing a shell script that's supposed to do the following. - run a Java application that produces output - run a shell command that produces output - gather both outputs and send them out in an email I have control of the source code of all the

  • Best way to read output of shell command 2012-02-05

    In Vim, What is the best (portable and fast) way to read output of a shell command? This output may be binary and thus contain nulls and (not) have trailing newline which matters. Current solutions I see: Use system(). Problems: does not work with NU

  • Emacs and Long Shell Commands 2010-04-24

    Is there a way to run a shell command, have the output show up in a new buffer and have that output show up incrementally? Eshell and other emacs terminal emulators do a find job of this but I see no way to script them. What I'd like to do is write l

  • How to execute shell command in C? 2011-05-12

    I am having problems executing the shell commands in C. I want to execute a shell command in C then capture the output of shell command and process the output further. I used following code to perform the functionality. But, the issue is when the she

  • Multiple asynchronous shell-commands in Emacs-Dired? 2011-08-01

    Emacs obviously can handle multiple asynchronous sub-processes, otherwise a multi-language programming environment like org-babel, to give an example, wouldn't be possible. However, when I'm in Dired and start an asynchronous shell command to view a

  • How do I programatically invoke a Felix/Karaf shell command? 2012-02-01

    I want to automatically invoke the Karaf "dev:watch" command if I detect that I'm running in a dev environment. I've considered adding dev:watch * directly to etc/shell.init.script but I don't want it to run unconditionally. So, I'm considering creat

  • How to pipe Bash Shell command's ouput line by line to Perl for Regex processing? 2012-02-07

    I have some output data from some Bash Shell commands. The output is delimited line by line with "\n" or "". I would like to know that is there any way to pipe the output into Perl and process the data line by line within Perl (just like piping the o

  • Passing Emacs variables to minibuffer shell commands 2012-04-12

    I can run a shell command quickly by hitting M-!. One thing I'd like to do is perform shell quick operations on the current file. An example would be checking the file out through perforce: M-! p4 edit buffer-file-name RET (Yes there are perforce int

  • define aliases for shell-command? 2010-09-05

    Is there a way to define aliases so that shell-command can recognize them? shell recognizes those defined in .emacs.d/; is it possible to make shell-command aware of these also? --------------Solutions------------- Read in simple.el in sh

  • How To Programmatically Wait For Shell Command To Finish Running? 2011-05-11

    I am sending emails in a program like so: Call Shell(smtpPath, emailInput...) This works great, except if I call the function twice, the function runs the first time, calls the shell command, then the function runs again, and calls the shell command

  • How to Pass MULTIPLE filenames to a Context Menu Shell Command? 2011-06-22

    Passing a single filename to a context menu shell command is simple: [HKEY_CLASSES_ROOT\*\shell\MyProgram\Command] @="program.exe %1" But if I select multiple files, program.exe is invoked for each such selected file. What I would like to do instead

Copyright (C), All Rights Reserved.

processed in 0.090 (s). 11 q(s)