cannot pass a echo output to a variable in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cannot pass a echo output to a variable in bash
# 1  
Old 07-17-2012
cannot pass a echo output to a variable in bash

Hi,

I have a problem with passing a echo output into a variable in bash

Code:
file='1990.tar'
NAME='echo $file | cut -d '.' -f1';
echo $NAME

the result is
Code:
echo $file | cut -d . -f1

however with this one,
Code:
#!/bin/bash
file='1990.tar'
echo $file | cut -d '.' -f1

the result is what I need:
Code:
1990

How could I pass the right filename 1990 to the variable "NAME", thanks!
# 2  
Old 07-17-2012
Code:
file='1990.tar'
NAME=$(echo "${file}" | cut -d '.' -f1)
echo "${NAME}"

The $( ) notation is preferred.


The problem in your original script was that you used single quotes not backticks to enclose the executable pipeline.
Code:
file='1990.tar'
NAME=`echo $file | cut -d '.' -f1`
echo $NAME

This User Gave Thanks to methyl For This Post:
# 3  
Old 07-17-2012
It's your quoting. You probably intended to use back ticks, but this is better.

Code:
NAME=$( echo $file | cut -d '.' -f1 )

You could also do it without having to invoke another process (best):

Code:
NAME="${file%.*}"

The syntax %.* causes the expansion of the variable to be truncated starting with the right most dot (.). Thus abc.def becomes just abc.
This User Gave Thanks to agama For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pass bash variable to python

How can I pass bash Variable to python script. bash.sh while read -r db do Printf "%s\n" ${db} "Found" done < path/to/file.txt file.txt db1 db2 db3 python.py print(${db}_tables.replicate.fix) (2 Replies)
Discussion started by: lpoolfc
2 Replies

2. UNIX for Beginners Questions & Answers

Can't pass a variable representing the output of lsb_release to a docker dontainer

I don't know why, but the rendering of my code mucks up the spacing and indentation, despite being correct in the original file. I'm having issues getting the following script to run (specifically the nested script at the end of the docker command near the end of the script; I think I'm not passing... (2 Replies)
Discussion started by: James Ray
2 Replies

3. Shell Programming and Scripting

How to read the output of a command line by line and pass it as a variable?

Hi, I have some 2000 names in a table like below. Java Oracle/SQL ANSI SQL SQL,DWH,DB DB&Java And by using for loop in my code i am able to get a single word but if there is any special character or space then it is considering as a next line. I have to execute the below queries in... (10 Replies)
Discussion started by: Samah
10 Replies

4. Homework & Coursework Questions

How to Dynamically Pass Parameter to plsql Function & Capture its Output Value in a Shell Variable?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 2. Relevant commands, code, scripts, algorithms: #! /bin/ksh v="ORG_ID" ... (2 Replies)
Discussion started by: sujitdas2104
2 Replies

5. Shell Programming and Scripting

BASH - Need to echo for loop output to one line

I'm trying to echo the release version of some of our Linux servers. Typically I do these types of things by "catting" a text file with the host names, "ssh-ing" to the host and running my string. This is what I've written for i in `cat versions.txt` ; do echo $i ; ssh $i cat /etc/issue |... (5 Replies)
Discussion started by: lombardi4851
5 Replies

6. Shell Programming and Scripting

Echo awk output from its variable

Stumped with the formatting of the awk output when used with variables, e.g.: awk -F, 'BEGIN {OFS=","} print {$2,$3,$4}' $infile1 produces the desired output (with rows), but when echoing the variable below, the output is one continuous line var1=$(awk -F, 'BEGIN {OFS=","} print... (4 Replies)
Discussion started by: ux4me
4 Replies

7. Shell Programming and Scripting

Sending Sed/Echo output to Variable

I have a variable $WORDS that contains a string Then i want to use sed to break it up. echo $WORDS | sed 's// /g' I tried setting this as a variable by doing WORDS2=`echo $WORDS | sed 's// /g'` But when i do this it does not return me to the prompt properly ie. jmpprd-v1> jmpprd-v1>... (4 Replies)
Discussion started by: nitrobass24
4 Replies

8. Shell Programming and Scripting

storing output from echo & cut into variable

Hi All, Hope someone can advise here as I have been struggling to find a syntax that works here. I have tried a stack of combination I have seed in the forums but I think because I have needed to use "" and `` in the statments another method is found. I am reading in lines with the following... (1 Reply)
Discussion started by: nkwilliams
1 Replies

9. Shell Programming and Scripting

[csh] How to capture output from a command and pass it on to a variable?

Hi there! I'm trying to write a script that will capture output from a command and assign it to a variable. Let's say, for example, I'd like to catch from inside the script whatever the following command outputs: ls *.aaa and put it into a variable "listoffiles". What I tried was: set... (3 Replies)
Discussion started by: machinogodzilla
3 Replies

10. Shell Programming and Scripting

Passing output of sed/echo to a variable

I understand how to use a variable in a sed command, but for the life of me I can't get the output into a variable. I'm making a general function to replace part of a filename with a different string, so: >>myscript this that would change: this_file001.txt to that_file001.txt and... (11 Replies)
Discussion started by: donflamenco
11 Replies
Login or Register to Ask a Question