Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8068803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:48:46+00:00 2026-06-05T12:48:46+00:00

Are there objectively better ways to create temporary files in bash scripts? I normally

  • 0

Are there objectively better ways to create temporary files in bash scripts?

I normally just name them whatever comes to my mind, such as tempfile-123, since it will be deleted when the script is over. Is there any disadvantage in doing this other than overwriting a possible tempfile-123 in current folder? Or is there any advantage in creating a temporary file in a more careful way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:48:48+00:00Added an answer on June 5, 2026 at 12:48 pm

    The mktemp(1) man page explains it fairly well:

    Traditionally, many shell scripts take the name of the program with
    the pid as a suffix and use that as a temporary file name. This kind
    of naming scheme is predictable and the race condition it creates is
    easy for an attacker to win. A safer, though still inferior, approach
    is to make a temporary directory using the same naming scheme. While
    this does allow one to guarantee that a temporary file will not be
    subverted, it still allows a simple denial of service attack. For
    these reasons it is suggested that mktemp be used instead.

    In a script, I invoke mktemp something like

    mydir=$(mktemp -d "${TMPDIR:-/tmp/}$(basename $0).XXXXXXXXXXXX")
    

    which creates a temporary directory I can work in, and in which I can safely name the actual files something readable and useful.

    mktemp is not standard, but it does exist on many platforms. The “X”s will generally get converted into some randomness, and more will probably be more random; however, some systems (busybox ash, for one) limit this randomness more significantly than others


    By the way, safe creation of temporary files is important for more than just shell scripting. That’s why python has tempfile, perl has File::Temp, ruby has Tempfile, etc…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way in objective-c/Cocoa to alloc an object when the class name
I'm wondering if there is a better way to approach this problem. The objective
I'm starting to understand memory management better in objective-c, but there's something I don't
I just found that there's no LinkedList implementation in Objective-C, but I need one.
Is this a bad idea? Is there a better way to achieve the same
I am trying to add/replace album art of MP3 files. Is there an Objective-C
Is there a better way in Objective-C to do: if ([elementName isEqual:@one]){ // do
There's a feature of the Apple Objective-C language which is really useful to me:
In Objective-C on Apple there is something called Key-Value Coding that allows you to
There are alternatives to developing for iPhone besides the native Objective-C API. In particular,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.