The Cookie Machine - Click here to drag window

DUMMY TEXT - Real text set in assets/js/theCookieMachine.js

Views: 5,588β€…    Votes:  16β€…    βœ… Solution
Tags: command-line   bash   redirect  
Link: πŸ” See Original Question on Ask Ubuntu πŸ”—

URL: https://askubuntu.com/q/1168750
Title: How to pass 2>/dev/null as a variable?
ID: /2019/08/27/How-to-pass-2__dev_null-as-a-variable_
Created: August 27, 2019    Edited:  August 28, 2019
Upload: November 24, 2022    Layout:  post
TOC: false    Navigation:  false    Copy to clipboard:  false


I have this code that works:

# Hide irrelevant errors so chrome doesn't email us in cron
if [[ $fCron == true ]] ; then
    google-chrome --headless --disable-gpu --dump-dom \
        "$RobWebAddress" > "$DownloadName" 2>/dev/null
else
    # Get silly error messages when running from terminal
    google-chrome --headless --disable-gpu --dump-dom \
        "$RobWebAddress" > "$DownloadName"
fi

If I try to shorten it like this:

# Hide irrelevant errors so chrome doesn't email us in cron
local HideErrors
[[ $fCron == true ]] && HideErrors="2>/dev/null"

google-chrome --headless --disable-gpu --dump-dom \
    "$RobWebAddress" > "$DownloadName" "$HideErrors"

I get error messages:

[0826/043058.634775:ERROR:headless_shell.cc(597)] Open multiple tabs is only supported when remote debugging is enabled.
[0826/043058.672587:ERROR:headless_shell.cc(597)] Open multiple tabs is only supported when remote debugging is enabled.
[0826/043058.711640:ERROR:headless_shell.cc(597)] Open multiple tabs is only supported when remote debugging is enabled.
(... SNIP ...)

Why does a hard-coded argument work but not an argument as a variable?


Edit 2:

Currently I found success with second answer’s alternate suggestion:

# Redirect errors when cron is used to /dev/null to reduce emails
ErrorPipe=/dev/stderr
[[ $fCron == true ]] && ErrorPipe=/dev/null

google-chrome --headless --disable-gpu --dump-dom \
                "$RobWebAddress" > "$DownloadName" 2>"$ErrorPipe"
## ```



### Edit 1:

Based on the first answer, I should point out program header already contains:

``` bash
[[ $fCron != true ]] &&
    exec 2> >(grep -v 'GtkDialog mapped without a transient parent' >&2)
⇧ Expand $PATH problem $HOME/bin is set twice Install DPKG package without updating and upgrating the whole system  β‡©