TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Unix Powerful Command – Eval
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Linux Tutorials

Unix Powerful Command – Eval

Last updated: Feb 24, 2024 11:06 pm
By Harsh S.
Share
4 Min Read
How to use eval command in Unix
How to use eval command in Unix
SHARE

The eval command is one of the most powerful and flexible commands in Linux. It combines all the given arguments and evaluates the combined expression and executes it, and returns the exit status of the executed command.

This command is commonly used to execute arguments as a shell command on the Linux system. Eval in <bash> works in essentially the same way as in most other languages that have an eval function. Perhaps the easiest way to think about the eval command is that it acts in the same manner as running the <bash -c “bash code…”> from a script.

In the below section, you’ll see a real-time example of the eval command. But you should understand that the Linux OS provides numerous commands which can help you in automating a lot of your tasks.

If you wish to know about all these essential Linux commands, then do follow this post, “Linux Commands for Beginners“.

The Eval Command Syntax

eval is useful when a command contains something which needs to be evaluated by the shell. The typical case would be when you have a variable containing the index of an argument you want to access.

eval: eval [arg ...]
    Execute arguments as a shell command.
Combine ARGs into a single string, use the result as input to the shell,
    and execute the resulting commands.
Exit Status:
    Returns exit status of command or success if command is null.

Watch eval command in action

Let’s now begin with a real-time use case of the eval command to fully explain its usage. It’ll also let us understand the point that how can we use it to fix our problem scenario.

So, in our use case, there is a set of three variables say DIR, Username (UN), and TIME. Each of these variables is postfixed by the index number. And there can be N number of such sets.

The challenge is to perform the following operations.

1. Enumerate through all these sets in a loop.
2. Dynamically postfix the index no to these variables.
3. Evaluate their actual values at run-time.
4. Print the actual value of the evaluated variable.

#!/bin/bash
#Used bash shell to support for loop syntax used below

#The no of times you want to iterate
count=3

#add any no of variables you wish to print in loop
DIR1=/home/a
UN1=Harsh1
Time1=10

DIR2=/home/b
UN2=Harsh2
Time2=20

DIR3=/home/c
UN3=Harsh3
Time3=30

echo
#for loop to run based on count variable
for (( i=1; i<=$count; i++ ))
do
  $(eval echo DIR$i))

Final Thought – The Eval Command

We hope the information about the eval command would be beneficial for you. The mere objective of writing about this Shell command was to make you aware of the sheer power of its features. So that you also use it with ease in your shell scripts. If you have a better example of this command, please share it with us by leaving your comments in the comment box section.

Thanks to all of you for reading this article. We’ll continue delivering shell scripting tutorials to make you master the art of scripting.

If this post succeeds in drawing your interest, then help others by sharing it. 🙂

Best,

You Might Also Like

Basic Linux Commands for Beginners With Examples

How to Use Bash to Replace Character in String

Simple Bash Scripting Tutorial for Beginners

20 Bash Script Code Challenges for Beginners with Their Solutions

Difference Between Git Repository and Directory

TAGGED:Unix Commands and Concepts
Harsh S. Avatar
By Harsh S.
Follow:
Hello, I'm Harsh, I hold a degree in Masters of Computer Applications. I have worked in different IT companies as a development lead on many large-scale projects. My skills include coding in multiple programming languages, application development, unit testing, automation, supporting CI/CD, and doing DevOps. I value Knowledge sharing and want to help others with my tutorials, quizzes, and exercises. I love to read about emerging technologies like AI and Data Science.
Previous Article C Programming Tips and Tricks. C Programming Tips and Tricks for Beginners
Next Article Selenium Tutorial - Page Object Model with Example. Implement Page Object Model Using Selenium WebDriver 2.0

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x