"why should eval be avoided in bash, and what should i use instead?" Code Answer
Answers related to “why should eval be avoided in bash, and what should i use instead?”
- The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead
- When and why should $_REQUEST be used instead of $_GET / $_POST / $_COOKIE?
- Why should session_write_close be used in long polling?
- How and where should I use the keyword “use” in php
- What is XML good for and when should i be using it?
- What should I use for user authentication in PHP?
- Why should I be using testing frameworks in PHP?
- What architecture should I use for writing my first dynamic website in PHP? [closed]
- Why should I use MVC in a php website? [closed]
- What can I use instead of eval()?
- What is the purpose of the var keyword and when should I use it (or omit it)?
- What is a smart pointer and when should I use one?
- Why should there be spaces around '[' and ']' in Bash?
- What does “use strict” do in JavaScript, and what is the reasoning behind it?
- When should I use h:outputLink instead of h:commandLink?
- What is the meaning of the prefix N in T-SQL statements and when should I use it?
- What does servletcontext.getRealPath(“/”) mean and when should I use it
- I've Heard Global Variables Are Bad, What Alternative Solution Should I Use?
- What is a “span” and when should I use one?
- Why should I not include cpp files and instead use a header?
- What is the difference between id and class in CSS, and when should I use them?
- What are the differences between numpy arrays and matrices? Which one should I use?
- Why is it not advisable to use attach() in R, and what should I use instead?
- What column type/length should I use for storing a Bcrypt hashed password in a Database?
- When should I use double instead of decimal?
- When should I use MySQLi instead of MySQL?
- Should I use <i> tag for icons instead of <span>? [closed]
- When should I use a struct instead of a class?
- When and how should I use a ThreadLocal variable?
- How does Cloneable work in Java and how do I use it?
- What parameters should I use in a Google Maps URL to go to a lat-lon?
- Java: Why is the Date constructor deprecated, and what do I use instead?
- About .bash_profile, .bashrc, and where should alias be written in?
- What is an MvcHtmlString and when should I use it?
- What is array literal notation in javascript and when should you use it?
- Arguments.callee is deprecated - what should be used instead?
- Android: When should I use a Handler() and when should I use a Thread?
- When should I use malloc in C and when don't I?
- In what order should I send signals to gracefully shutdown processes?
- Is `shouldOverrideUrlLoading` really deprecated? What can I use instead?
- c# When should I use List and when should I use arraylist?
- Observer is deprecated in Java 9. What should we use instead of it?
- What kind of OCR Java library should I use in Android? [closed]
- Can ${var} parameter expansion expressions be nested in bash?
- What can I use instead of the arrow operator, `->`?
- eval command in Bash and its typical uses
- When should I use Memcache instead of Memcached?
- Should I use .done() and .fail() for new jQuery AJAX code instead of success and error
- Why can't I use job control in a bash script?
- When should I use jQuery deferred's “then” method and when should I use the “pipe” method?
- When and how should I use exception handling?
- Eval is evil… So what should I use instead?
- What is self in ObjC? When should i use it?
- What tag in HTML should I use without special meaning, only to carry meta data?
- In Python, when should I use a function instead of a method?
- In ASP.NET, when should I use Session.Clear() rather than Session.Abandon()?
- Why should I practice Test Driven Development and how should I start?
- Is the use of std::vector<bool> objects in C++ acceptable, or should I use an alternative?
- Why most of the time should I use const instead of let in JavaScript?
- java.net.URLEncoder.encode(String) is deprecated, what should I use instead?
- What should I use instead of syncdb in Django 1.9?
- Shebang line limit in bash and linux kernel
- What is aria-label and how should I use it?
- Should Tables be avoided in HTML at any cost?
- Should I use return/continue statement instead of if-else?
- Should I use a Shebang with Bash scripts?
- Can parameter expansion be nested in Bash?
- What version of Python should I use if I'm a new to Python?
- What are expressions with side effects and why should they be not passed to a macro?
- What does the UseCompressedOops JVM flag do and when should I use it?
- When should I use Import-Package and when should I use Require-Bundle?
- Python: Why should I use next() and not obj.next()?
- What is pyximport and how should I use it?
- GUIText is deprecated, so what should I use instead of it?
- I would like to select a DropDown from a list but HTML don't have select Tag I am not sure should i use Select...
- What values should I use for CFBundleVersion and CFBundleShortVersionString?
- When should i use npm with “-g” flag and why?
- What is uint_fast32_t and why should it be used instead of the regular int and uint32_t?
- What's the difference between #import and @class, and when should I use one over the other?
- What data type should I use to represent money in C#?
- How do you store a list of directories into an array in Bash (and then print them out)?
- How do I print some text in bash and pad it with spaces to a certain width?
- why in an 'if' statement 'then' has to be in the next line in bash?
- What's the standard way to work with dates and times in Scala? Should I use Java types or there are native Scala...
- When should I use Sql Azure and when should I use table Storage?
- Best practices: XML attribute vs XML element - When should I use elements and when should I use attributes?
- Should I use uint in C# for values that can't be negative?
- What is double evaluation and why should it be avoided?
- What should I do to use Task<T> in .NET 2.0?
- What should i use for better performance, nine-patch or drawable xml resource?
- What do I use instead of Whitebox in Mockito 2.2 to set fields?
- Quoting in bash and perl in recursive ssh command
- Why is my string comparison in Bash always evaluated as true?
- What are the differences between CV_8U and CV_32F and what should I worry about when converting between them?
- What should I use to replace gettimeofday() on Windows?
- When should I use a Map instead of a For Loop?
- In Angular rxjs when should I use `pipe` vs `map`
- In what cases should I use memcpy over standard operators in C++?
- What should I use an IEnumerable or IList?
- Is there possible to use createQueryBuilder for insert/update? If not, what function should I use?
- What is a fulltext index and when should I use it? [closed]
- What is a DSL and where should I use it?
- When should I use primitives instead of wrapping objects?
- What should I use instead of sscanf?
- When should I use vector<int>::size_type instead of size_t?
- Compiler can't find Py_InitModule() .. is it deprecated and if so what should I use?
- What wrapper class in C++ should I use for automated resource management?
- When should I use Apache Commons' Validate.isTrue, and when should I just use the 'assert' keyword?
- When should I use NSURL instead of NSString and vice versa?
- What should I use to compare DBNull ? Using DBNull.Value or ToString().IsNullOrEmpty()
- Why is Spring de-coding + (the plus character) on application/json get requests? and what should I do about it?
- jq reformatting decimals in scientific notation — can this be avoided?
- Should I use HSV/HSB or RGB and why?
- What should I use for a BigInt class in .NET?
- What should I use instead of toPromise() when using await on an Observable?
- Why does Spring Integration have several XML schemas, and which one should I use?
- What CSS should I use to get a border around an option tag in both Firefox and IE?
- How and when should I use pitched pointer with the cuda API?
- Do python projects need a MANIFEST.in, and what should be in it?
- Should I use quotes in environment path names?
- When should I use enhanced record types in Delphi instead of classes?
- TWRequest is deprecated in iOS 6.0 - what can I use instead?
- transactionReceipt for in-app purchase is deprecated in iOS 7. What can I replace it with?
- sign in with oAuth, what should i store/use to identify the user?
- When should I use UIImagePickerControllerSourceTypePhotoLibrary instead of...
- In what circumstances should I use instance variables instead of other variable types?
- Merging 8 sorted lists in c++, which algorithm should I use
- What should I use instead of deprecated GKLeaderboardViewController in iOS7?
- Python: what is the difference between (1,2,3) and [1,2,3], and when should I use each?
- “event” is deprecated, what should be used instead?
- What if I use #! instead of # in href of anchor tag <a>?
- Should I use a global variable and if not, what instead? (Javascript)
- In Objective-C, when should I use property and when should I use instance variable?
- What should I use to display game graphics?
- When should I use __forceinline instead of inline?
- Getting screen width on API Level 30 (Android 11): getDefaultDisplay() and getMetrics() are now deprecated. What...
- Parse CSV in bash and assign variables
- Should I use threeten instead of joda-time
- What tag should I use instead of deprecated tag font in html (cannot use CSS)
- Why should I be making my page objects instantiated rather than static?
- How do I learn how to get quoting right in bash?
- should I avoid bash -c, sh -c, and other shells' equivalents in my shell scripts?
- What can I use instead of Context Menu in android? [closed]
- When should I use Perl CGI instead of PHP (or vice versa)?
- What is the Javascript method CollectGarbage()? When and why should it be used?
- When should I use a UserControl instead of a Page?
- Why does [:lower:] return differently in bash depending on the existence of files?
- What should I do to get the whole return value of c-program from command line?
- TcpClient.Connected returns true yet client is not connected, what can I use instead?
- what reference should I use to use erf / erfc function
- What should be used instead of the deprecated EntityName.account.ToString()?
- ngStyle VS Renderer2 ? What should I use?
- For temporary registers in the asm statement, should I use clobber or dummy output?
- What are the folders in the lib folder in Glassfish for, and how should you use them?
- What is an assumed length character function result, and why should it be avoided?
- What should I use as target vector when I use BinaryCrossentropy(from_logits=True) in tensorflow.keras
- What RegEx should I use to split a string according to 2 word format and spaces?
- Which datatype and methods should I use?
- When should I use XML-RPC instead of a Controller in Odoo?
- What is the purpose of #pragma aux and how should I use it?
- Press alt + numeric in bash and you get (arg [numeric]) what is that?
- Why should I use 'apply' in Clojure?
- Should I use PhoneGap instead of ramping up on the Android SDK?
- I'm new to .NET - what should I concentrate on and what should I ignore? [closed]
- What is the difference between DatatypeProperty, ObjectProperty, & FunctionalProperty, and when should I use them?
- What does <f:facet> do and when should I use it?
- In a “for” statement, should I use `!=` or `<`?
- Should I use class method or instance method, and why?
- What language should I use to write a text parser and display the results in a user friendly manner? [closed]
- Monitoring Celery, what should I use? [closed]
- AWS Cognito: Difference between Cognito ID and sub, what should I use as primary key?
- What libraries should I use for linear programming in python? [closed]
- What does the @elidable annotation do in Scala, and when should I use it?
- echo outputs -e parameter in bash scripts. How can I prevent this?
- What are docValues in Solr? When should I use them?
- What size of screenshot should I use in the Android Market?
- When should I use JSF components and when should I use html tags? [closed]
- When should I use SHA-1 and when should I use SHA-2?
- What is the purpose of ROWLOCK on Delete and when should I use it?
- When should I use a Localize control instead of a Literal?
- “Double” assignment - should it be avoided?
- Should MEDIUMINT be avoided in MySQL?
- When and how should I use enumeration classes rather than enums?
- What Quotation Marks Should I Use In CSS?
- In VBA should code that modifies document be avoided in a class module
- Should I use ASP.NET sessions or avoid them and why?
- What mock object framework should I use when developing in Python on the Google App Engine?
- What server-side web technology should I use in an embedded system?
- When should I use C++ instead of SQL?
- Element “align” is obsolete or non-standard: what should I use instead?
- What method can I use instead of __file__ in python?
- Bash – How should I idle until I get a signal?
- UIBarButtonItemStyleBordered is deprecated; what should I do, instead?
- Why can't I use attr_accessor inside initialize?
- What is the difference between __set__ and __setattr__ in Python and when should which be used?
- what context should i use AlertDialog.Builder in?
- What should I use in Android when porting C++ code written with libsndfile?
- C# - What should I use, an Interface, Abstract class, or Both?
- Is it a bad practice to use state in a React component if the data will not change? Should I use a property on the...
- What type of components should I use in React: functional components or class base components?
- How to store tables in C# - What kind of data structure should I use?
- How to make requests in third party APIs and load the results periodically on google BigQuery? What google services...
- when should I use a sorteddictionary instead of a dictionary
- In Ember 1.13 and later what key should I use with each when iterating over an array of strings?
- What is copyWith and how can I use it in Flutter and what is it's use case?
- Should I use constants instead of strings even if the strings are only ever used once?
- Why should addChangeListener be in componentDidMount instead of componentWillMount?
- When should I use Scala's Array instead of one of the other collections?
- What does @firebase/app-types@0.x do and when should I use it?
- Should i use iOS 4 new features in my app? and why?
- Should I use an In-Memory Database instead of mocking out my Repositories?
- Why does geany use #~ for comments in bash instead of just #?
- What can I use instead of the :indeterminate jQuery selector to avoid an exception in IE 8?
- What should I use to serialize a DataTable to JSON in ASP.NET 2.0?
- How can I use a custom function within an expression using the eval dataframe method?
- When should I use a class and when should I use a function?
- Kohana v3.1.0 ORM _ignored_columns -- now that it's gone, what should I do instead?
- When should I use "var" instead of "object"? [closed]
- if $(window).load() is deprecated, what should I use?
- What kind of API should I use to elevate user privilege to super-user in a Mac OS X?
- What Xcode settings should I use to support both armv6 and armv7?
- Oracle SQL. What statement should I use
- When should a call to *eval* be evaluated in a make recipe
- In x86 assembly, when should I use global variables instead of local variables?
- Should I use a Struct instead of a lightweight data class for my Linq2Sql data?
- Why can't I use $(...) in PS1 instead of backticks?
- In Java what should I use for a PriorityQueue that returns the greatest element first?
- Should I put the "eval" in the subroutine or the subroutine in the "eval"?
- How do I simplify bash's 'eval "$TIME $BIN_FILE $BIN_OPTS &> $LOG_FILE"' and keep it working?
- ParseKit: What built-in Productions should I use in my Grammars?
- Why Anaconda has separate packages for Tensorflow with and without GPU, and should I use conda or pip?
- Why should I use LabelFor in MVC?
- In what situation should I use ASCII to transfer a file over FTP? (I'm not asking the diff between ascii xfer and...
- What's the difference between Android's Html.escapeHtml and TextUtils.htmlEncode ? When should I use one or the other?
- What should I prefer to use widget or renderPartial in Yii's view?
- Why is the '@' symbol reserved in javascript and what is its purpose?
- If window.navigator.userAgent is deprecated, what should I use instead?
- What should I use instead of printf in Perl?
- Which should I use for empty string and why?
- C#: In a KeyDown event, what should I use to check what key is down?
- What should I use for password fields in a table; MD5 or SHA1?
- Rounded Textured Segmented control style deprecated in OSX 10.7? What should I use?
- When should I use subprocess.Popen instead of os.popen?
- When should I use StringComparison.InvariantCulture instead of StringComparison.CurrentCulture to test string...
- Why does the evaluation of a string defining a function return undefined instead of the function?
- Should I use URLs as links to images and other resources or should I use the EL #{resource...}
- What control should I use to create this UI in Delphi Firemonkey
- What Request URL for Voice in TwiML App setup should I use when I develop on localhost?
- OSGi and ...? What do I use to controll loading/unloading/reloading of packages?
- What seed value should I use for erand48() in C?
Only authorized users can answer the Search term. Please sign in first, or register a free account.
there's more to this problem than meets the eye. we'll start with the obvious:
eval
has the potential to execute "dirty" data. dirty data is any data that has not been rewritten as safe-for-use-in-situation-xyz; in our case, it's any string that has not been formatted so as to be safe for evaluation.sanitizing data appears easy at first glance. assuming we're throwing around a list of options, bash already provides a great way to sanitize individual elements, and another way to sanitize the entire array as a single string:
now say we want to add an option to redirect output as an argument to println. we could, of course, just redirect the output of println on each call, but for the sake of example, we're not going to do that. we'll need to use
eval
, since variables can't be used to redirect output.looks good, right? problem is, eval parses twice the command line (in any shell). on the first pass of parsing one layer of quoting is removed. with quotes removed, some variable content gets executed.
we can fix this by letting the variable expansion take place within the
eval
. all we have to do is single-quote everything, leaving the double-quotes where they are. one exception: we have to expand the redirection prior toeval
, so that has to stay outside of the quotes:this should work. it's also safe as long as
$1
inprintln
is never dirty.now hold on just a moment: i use that same unquoted syntax that we used originally with
sudo
all of the time! why does it work there, and not here? why did we have to single-quote everything?sudo
is a bit more modern: it knows to enclose in quotes each argument that it receives, though that is an over-simplification.eval
simply concatenates everything.unfortunately, there is no drop-in replacement for
eval
that treats arguments likesudo
does, aseval
is a shell built-in; this is important, as it takes on the environment and scope of the surrounding code when it executes, rather than creating a new stack and scope like a function does.eval alternatives
specific use cases often have viable alternatives to
eval
. here's a handy list.command
represents what you would normally send toeval
; substitute in whatever you please.no-op
a simple colon is a no-op in bash:
create a sub-shell
execute output of a command
never rely on an external command. you should always be in control of the return value. put these on their own lines:
redirection based on variable
in calling code, map
&3
(or anything higher than&2
) to your target:if it were a one-time call, you wouldn't have to redirect the entire shell:
within the function being called, redirect to
&3
:variable indirection
scenario:
bad:
why? if ref contains a double quote, this will break and open the code to exploits. it's possible to sanitize ref, but it's a waste of time when you have this:
that's right, bash has variable indirection built-in as of version 2. it gets a bit trickier than
eval
if you want to do something more complex:regardless, the new method is more intuitive, though it might not seem that way to experienced programmed who are used to
eval
.associative arrays
associative arrays are implemented intrinsically in bash 4. one caveat: they must be created using
declare
.in older versions of bash, you can use variable indirection: