LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-25-2017, 05:22 AM   #1
lobsang
LQ Newbie
 
Registered: Mar 2017
Posts: 11

Rep: Reputation: Disabled
set a variable to the output from a command in Bash


can any one explain me how it works.

mydate ="$(date)"
 
Old 03-25-2017, 05:43 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,730

Rep: Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920
Welcome to LinuxQuestions. Homework?

It is called command substitution. You can find lots of info online but here you go

https://bash.cyberciti.biz/guide/Command_substitution
https://www.gnu.org/software/bash/ma...stitution.html

Last edited by michaelk; 03-25-2017 at 05:52 AM.
 
Old 03-25-2017, 04:53 PM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
it won't work with that spacing.
please use code tags for code.

also, does your cat speak tibetan? ;-)
 
Old 03-26-2017, 02:21 PM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,805

Rep: Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204
Code:
mydate="$(date)"
(No spaces around the equal sign!)
$() means it runs the date command in a subshell, and its standard output becomes a string that is assigned to the mydate variable.
The ""quotes protect the string from eventual word splitting or substitution - not needed in an assignment; you can omit them.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash script: How to assign variable to an sqlite3 command with variable embedded? ninja6o4 Linux - Software 10 02-15-2015 04:43 PM
[BASH] Assign output to variable(s) useretail Linux - Newbie 4 12-06-2014 06:08 AM
[SOLVED] Java output into bash variable Centi66 Programming 11 08-15-2014 10:35 AM
set variable from output of apg takayama Programming 1 12-11-2011 05:27 AM
[SOLVED] Bash; awk or sed output to variable: how keep newline at end of each output line porphyry5 Programming 3 06-10-2011 05:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration