How to use a variable as a command option?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use a variable as a command option?
# 1  
Old 03-01-2012
How to use a variable as a command option?

I am just learning shell scripting and already I found out I have the bad habit of thinking that it is similar to php or c.

I learned some basics and now encountered this problem:

On shell it is possible to type:

 
$ date --date="2009-10-10 09:08:34"
Sat Oct 10 09:08:34 CEST 2009

This tells date to output the date in that string in that default format. But now, I can't pass that string as a variable:

 
$ dateLabel="2009-10-10 09:08:34"
$ date --date=$dateLabel
date: the argument `09:08:34' lacks a leading `+';
when using an option to specify date(s), any non-option
argument must be a format string beginning with `+'
Try `date --help' for more information.

I thought the problem could be the quotes but I have the same result if I first set the variable to have quotes:

 
$ dateLabel="\"2009-10-10 09:08:34\""
$ date --date=$dateLabel
date: the argument `09:08:34"' lacks a leading `+';
when using an option to specify date(s), any non-option
argument must be a format string beginning with `+'
Try `date --help' for more information.

I just don't get it, is it like there is a different encoding for that blank space?

Anyone can help me?
# 2  
Old 03-01-2012
Code:
$ dateLabel="2009-10-10 09:08:34"
$ date --date="$dateLabel"
Sat Oct 10 09:08:34 GMT 2009

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 03-01-2012
Thank you balajesuri, that works

That feels embarrasing..

Now in my defense I can tell that I did look for more info. I found out something that in this case works: to change IFS environment variable to somethin else, like

Quote:
$backup="$IFS"
$IFS=''
#script
$IFS="$backup"
Good that I didn't accept that risk and complexity as a "solution"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command option substitution?

Hi folks, I totally dislike asking questions in forums but this one eats up to much of my time I need to spend on other topics. I have a shell-script in which I call a terminal. I want to invoke bash inside the terminal and print a message inside bash with aid of a here document. See... (7 Replies)
Discussion started by: bluntroller
7 Replies

2. OS X (Apple)

What's the reason behind having -n option for mv command?

Sorry for a question that may seem dumb but learning UNIX basics I still can not grasp benefits of using mv -n source file target file I can understand the need for cp -n source file target file when you get a copy with contents untouched but the former baffles me. I know that this about... (8 Replies)
Discussion started by: scrutinizerix
8 Replies

3. Shell Programming and Scripting

Passing a variable value to an option

Hello, I am new to shell (i.e. linux bash) programming and have the following question: When using this wget command I can download a certain website that needs login information by passing a previously acquired cookie: wget --header='Cookie: SID=ac658ef0876b24ff456' somewebsite.comAs... (5 Replies)
Discussion started by: iggy98
5 Replies

4. Shell Programming and Scripting

Variable inside -name option for find command

Hi, I am not able to get output for find command if there are variables defined inside -name option . Please check below example 1) ###VARIABLES DEFINED process="fast" temperature="125c" voltage="0p935v" 2) I don't get output for below find command find -L <PATH> -type f \( -name... (2 Replies)
Discussion started by: gujrathinr
2 Replies

5. Shell Programming and Scripting

variable option not properly recognized

I'm creating a junk script which utilizes either -l or -p to list files or remove files, respectively, in the junk directory. When I run this code, inputting '-p' is unrecognized through the whole if/else block and falls to the last else (echo do nothing). In addition, I switched tests and tested... (2 Replies)
Discussion started by: haishuva
2 Replies

6. HP-UX

who command option not working

Running HP 11.31 on a HP3600. But when I log in as a user the who command works but if I use an option like "who -m" I get nothing. Any thoughts on what is causing this problem. (11 Replies)
Discussion started by: KMRWHUNTER
11 Replies

7. Shell Programming and Scripting

How can I use a pipe operator in a variable: OPTION="| command"?

I have a string of commands I am piping some data through and I want to allow command line switches to select which commands are used. I want to do something like this: OPTION="| command3" command1 -a -b c.txt | command2 -d -e $OPTION >result.txt I want to do it that way because OPTION may be... (1 Reply)
Discussion started by: KenJackson
1 Replies

8. Shell Programming and Scripting

Env Variable substituion in Sed (-s option)

Folks, I've been trying to use the ENV variable with slashes(/) in its value inside the sed substitution.. Sed 's/myval/'$MYVAL'/' file1 >> file.tmp If MYVAL=<sometext>, it works. if MYVAL=/home/venkat, it doesnt. *************************** bash-2.05$ export VAL=/home/venkat... (5 Replies)
Discussion started by: gvsreddy_539
5 Replies

9. UNIX for Dummies Questions & Answers

option for ls command

i'm using SunOS 5.7 and I know theres a ls option for seeing what kind of files are in a directory. I was wondering if there was a ls option that could see if the files are txt or files that can be opened in vi (1 Reply)
Discussion started by: eloquent99
1 Replies

10. Shell Programming and Scripting

-c option in ping command

What does '-c' mean in ping command? Is this option specific to bash shell? Deepa (3 Replies)
Discussion started by: Deepa
3 Replies
Login or Register to Ask a Question