How to use shell variables in perl command call in a bash shell script?
I have a perl command in my shell script to evaluate date -1.
How can i use $myDate
in perl command call?
This is the section in my script:
myDate='10/10/2012'
Dt=$(perl -e 'use POSIX;print strftime '%m/%d/%y', localtime time-86400;")
I want use $myDate
in place of %m/%d/%y
.
Any help will be appreciated.
Thank you.
The first line tells the system that this is a shell (/bin/sh) script. The next line tells the shell to execute the perl command with the –x and –S options. The $0 variable is the name of the program and ${1+"$@"} is a magic string that passes the command line arguments from the shell to the perl command.
Using variable from command line or terminal You don't have to use any special character before the variable name at the time of setting value in BASH like other programming languages. But you have to use '$' symbol before the variable name when you want to read data from the variable.
The easiest way to set environment variables in Bash is to use the “export” keyword followed by the variable name, an equal sign and the value to be assigned to the environment variable.
In this chapter, we will learn how to use Shell variables in Unix. A variable is a character string to which we assign a value. The value assigned could be a number, text, filename, device, or any other type of data. A variable is nothing more than a pointer to the actual data.
Variables from the shell are available in Perl's %ENV
hash. With bash
(and some other shells) you need to take the extra step of "exporting" your shell variable so it is visible to subprocesses.
mydate=10/10/2012
export mydate
perl -e 'print "my date is $ENV{mydate}\n"'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With