Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use shell variables in perl command call in a bash shell script?

Tags:

bash

shell

awk

perl

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.

like image 815
user836087 Avatar asked Oct 26 '12 20:10

user836087


People also ask

How do I run a Perl command in a shell script?

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.

How do you use variables in bash commands?

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.

How do I set the shell variable in bash?

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.

How do you call a variable in a shell script?

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.


1 Answers

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"'
like image 68
mob Avatar answered Oct 11 '22 17:10

mob