Trying to format SD card for raspberry pi, but I get permission denied

Below itemizes some of the problems with procedure posted in your question.

  • As others have posted in the comments and answers, you need to precede the fdisk command with sudo.
  • Other users have suggested that the security setting need to be modified. The version of macOS used for verifying this answer was Catalina version 10.15.3. Below is the System Preferences setting in the Security & Privacy pane. Under Full Disk Access, the Terminal application is not checked off, as shown below. Under Files and Folders, the following was checked off for Terminal.
  • The fdisk command in your question is not being applied to a drive. The identifier disk2s1 could represent a partition, container, session or volume. While the fdisk command can allow for this, there is no logical reason for doing so. The device should be of the form /dev/diskN where N is a positive integer. Care should be taken to confirm you are actually referring to the SD card.
  • The fdisk can be an interactive command. This means the command can require text read from standard input. This text is often referred to a input commands. Under Linux, fdisk defaults to interactive. However under macOS, fdisk does not. To make fdisk interactive under macOS, the ‑e option must be included. In your question, this option was omitted.
  • You tried to pipe the input commands to fdisk. Each individual command was separated by a space (). However, each command should have been separated by a newline (\n). Basically a newline is the same as entering a return from the keyboard. Also the string should have be enclosed by the characters $'' instead of "".
  • The input commands to fdisk presented in your questions would be valid if the operating system was Arch Linux. The equivalent commands which would be correct under macOS are given below.

    erase
    print
    edit 1
    c
    n
    2048
    204800
    edit 2
    83
    n
    
    
    quit
    y
    

The correcting for the above errors would lead to the command shown below. Here, the input commands have be abbreviated.

    echo $'er\np\ne 1\nc\nn\n2048\n204800\ne 2\n83\nn\n\n\nq\ny'|sudo fdisk -e /dev/disk2

Note: In the above command disk2 was used to identify the SD Card. You may need to use a different identifier.

Below is the output that should appear when executing the above command for your SD card.

fdisk: could not open MBR file /usr/standalone/i386/boot0: No such file or directory
Enter 'help' for information
fdisk: 1> fdisk:*1> Disk: /dev/disk2    geometry: 3891/255/63 [62521344 sectors]
Offset: 0   Signature: 0xAA55
         Starting       Ending
 #: id  cyl  hd sec -  cyl  hd sec [     start -       size]
------------------------------------------------------------------------
 1: 00    0   0   0 -    0   0   0 [         0 -          0] unused      
 2: 00    0   0   0 -    0   0   0 [         0 -          0] unused      
 3: 00    0   0   0 -    0   0   0 [         0 -          0] unused      
 4: 00    0   0   0 -    0   0   0 [         0 -          0] unused      
fdisk:*1>          Starting       Ending
 #: id  cyl  hd sec -  cyl  hd sec [     start -       size]
------------------------------------------------------------------------
 1: 00    0   0   0 -    0   0   0 [         0 -          0] unused      
Partition id ('0' to disable)  [0 - FF]: [0] (? for help) Do you wish to edit in CHS mode? [n] Partition offset [0 - 62521344]: [63] Partition size [1 - 62519296]: [62519296] fdisk:*1>          Starting       Ending
 #: id  cyl  hd sec -  cyl  hd sec [     start -       size]
------------------------------------------------------------------------
 2: 00    0   0   0 -    0   0   0 [         0 -          0] unused      
Partition id ('0' to disable)  [0 - FF]: [0] (? for help) Do you wish to edit in CHS mode? [n] Partition offset [0 - 62521344]: [206848] Partition size [1 - 62314496]: [62314496] fdisk:*1> Writing current MBR to disk.

The first input command was erase, therefore the output above contains mostly zeroes. To see the results, you would need enter the command given below.

fdisk /dev/disk2

After entering the above command, you should see the output shown below.

Disk: /dev/disk2    geometry: 3891/255/63 [62521344 sectors]
Signature: 0xAA55
         Starting       Ending
 #: id  cyl  hd sec -  cyl  hd sec [     start -       size]
------------------------------------------------------------------------
 1: 0C 1023 254  63 - 1023 254  63 [      2048 -     204800] Win95 FAT32L
 2: 83 1023 254  63 - 1023 254  63 [    206848 -   62314496] Linux files*
 3: 00    0   0   0 -    0   0   0 [         0 -          0] unused      
 4: 00    0   0   0 -    0   0   0 [         0 -          0] unused      

This raspberrypi.org website offers advice when certain error messages appear. I have repeated some of this advice below. You will need to replace diskN with the correct identifier.

  • If the command reports Resource busy, you need to unmount the drive first using the command given below.

    sudo diskutil unmountDisk /dev/diskN
    
  • If the command reports Operation not permitted you need to disable SIP before continuing.

    Note: I would strongly recommend disabling SIP as a last resort.

  • If the command reports Permission denied, the partition table of the SD card is being protected against being overwritten by macOS. Erase the SD card's partition table using this command:

    sudo diskutil partitionDisk /dev/diskN 1 MBR "Free Space" "%noformat%" 100%
    

    That command will also set the permissions on the device to allow writing.

IMO, I believe you should attempt to use Arch Linux instead of macOS. I tried installing the latest Arch Linux in a VirtualBox virtual machine. (VirtualBox is a a free product). However, I could not get the virtual machine to work with the builtin SD card reader on my iMac (21.5-inch, Late 2013). I had no better luck with an external USB SD card reader. Maybe I did not have the correct settings or maybe different hardware would make the difference. Also, I did not try using the sudo vboxmanage internalcommands createrawvmdk ... commands to expose the SD card to the virtual machine.

Instead of a virtual machine, I opted to try a live version of Arch Linux. Here, Arch Linux was able to access the external SD card reader, but not the internal reader. Live means you create a Arch Linux bootable USB flash drive. Once you boot from the flash drive, you can enter the commands to install Arch Linux on the SD card. The Arch Linux ISO file was name archlinux-2020.03.01-x86_64.iso and came from this website, although other mirrors can be found here.

The instructions for using macOS to create the bootable flash drive can be found here. An image taken from this website is shown below.

Note: My iMac has a wireless keyboard. Arch Linux failed to work with this keyboard. I had to resort to a wired keyboard.

I did hypothesize that your question came from a website such as this one. An image of the installation instructions is shown below.

In testing, I modified the above instructions so the downloaded file would be stored on the SD card. After installing this file can be deleted. To stored the file on the SD card, replace the commands given in step 5 with the commands shown below

cd root
wget http://os.archlinuxarm.org/os/ArchLinuxARM-rpi-4-latest.tar.gz
cd ..
bsdtar -xpf root/ArchLinuxARM-rpi-4-latest.tar.gz -C root
sync